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
     
    neilfurry, Jan 22, 2017 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    you are using php or javascript to output the code?
     
    sarahk, Jan 22, 2017 IP
  3. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #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.