javascripthtmltemplate-literalsbackticks

Multiple use of backticks within Javascript


I'm having some trouble using backticks and I'm wondering if anyone can help me. I have the code below which allows me to output multiple responses. The trouble is, when I put a comma after target="_blank">here` it doesn't allow me to add further phrases. I have attempted using a backspace before the backtick to break out of it but no luck. Here is my code, I'm using Javascript and HTML.

<script>
function talk(){
var know ={
        "How does this work":"I can help you find what you're looking for.",
        "how does this work":"I can help you find what you're looking for.",        

        "contact":`You can contact us by clicking <a href='https://addressname.com' target="_blank">here</a>` 
};  

var user = document.getElementById('userBox').value;
document.getElementById('chatLog').innerHTML = user + "<br>";
if(user in know){
    document.getElementById('chatLog').innerHTML = know[user] + "<br>";
}else{
    document.getElementById('chatLog').innerHTML = "I do not understand.";
}
}

</script>

To clarify, I'm needing something like this: (but obviously the comma doesn't work)

"How does this work":"I can help you find what you're looking for.",
        "how does this work":"I can help you find what you're looking for.",        

        "contact":`You can contact us by clicking <a href='https://addressname.com'target="_blank">here</a>`,
        "help":`You can find help by clicking <a href='https://addressname.com'target="_blank">here</a>`

Solution

  • As you know the use of " & ' should corresponded to each other

    Double quotes around attribute values are the most common in HTML, but single quotes can also be used.
    In some situations, when the attribute value itself contains double quotes, it is necessary to use single quotes:

    <p title='John "ShotGun" Nelson'>

    Or vice versa:

    <p title="John 'ShotGun' Nelson">

    The backslash (\) escape character turns special characters into string characters .
    If there are extra symbols in between you can use \ backslash before them so that they don't interfere with the code and treated as symbols like this :

    <p title="John \'ShotGun\' Nelson you\'re a good\\bad person">
    It will be displayed like this :
    John 'ShotGun' Nelson you're a good\bad person

    Refer to this for more about backslash