1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Including single quotes in an inline javascript

Discussion in 'JavaScript' started by neilfurry, Jan 22, 2017.

  1. #1
    Hello,
    I need help please,.. all i wanted is to have this output

    <span onclick="myfunction('1','2','3')">Link</span>

    my script is:

    <span class="clickable" onclick=\"myfunction("'+data.tid+'","'+data.startpay+'","'+data.endpay+'")\">' + data.emp + '</span>

    This doesn't seem to work right as when i look into my console the output is:

    <span class="clickable" onclick="myfunction(" 34","","")"="">Link</span>

    all i wanted is to output this like

    <span onclick="myfunction('1','2','3')">Link</span>

    Thank you

    SEMrush
     
    neilfurry, Jan 22, 2017 IP
    SEMrush
  2. sarahk

    sarahk iTamer Staff

    Messages:
    23,466
    Likes Received:
    3,166
    Best Answers:
    86
    Trophy Points:
    615
    #2
    you are using php or javascript to output the code?
     
    sarahk, Jan 22, 2017 IP
  3. hdewantara

    hdewantara Active Member

    Messages:
    402
    Likes Received:
    33
    Best Answers:
    17
    Trophy Points:
    80
    #3
    @sarahk:
    javascript, since (s)he is using '+' to concatenate strings, not '.'

    @neilfurry:
    why don't use a bit of modern way to inject <span> ?
    untested, but it looks something like below:
    var span = target_element.appendChild(document.createElement('span'));
    span.appendChild(document.createTextNode(data.emp));
    span.className = 'clickable';
    span.addEventListener('click', function(){
        myfunction(data.tid, data.startpay, data.endpay);
    }, false);
    Code (JavaScript):
    it's lenghtier, but tidier :)
     
    hdewantara, Jan 22, 2017 IP
    sarahk likes this.