user188995
user188995

Reputation: 557

Add onClick event

I'm having real trouble adding an onClick event to a script. Here's my code that basically shows Instagram photos. I want to add an onclick="panel_five.show();return false" to

<a href='" + data.data[i].images.standard_resolution.url +"' >
    <img src='" + data.data[i].images.thumbnail.url +"' />
</a>

I'm having real trouble in syntax. Here's my complete code:

...
$('.ttl').first().parent().append('<div class="ttl"><div class="ttlpadding"><div class="item">' + "<a href='" + data.data[i].images.standard_resolution.url +"' ><img src='" + data.data[i].images.thumbnail.url +"' /></a>" +"</div></div></div>");
...

Upvotes: 0

Views: 182

Answers (3)

hsgu
hsgu

Reputation: 864

try this I have test with my url and image src and it work fine

$('.ttl').first().parent().append('<div class="ttl"><div class="ttlpadding"><div class="item">' + "<a href='" + data.data[i].images.standard_resolution.url +"' ><img src='" + data.data[i].images.thumbnail.url +"' onclick='panel_five.show();return false;' /></a>" + '</div></div></div>');

Edit: If you have confused with " and ' you can use " and \" instead OR ' and \' as well

$("body").append("<a href=\"http://stackoverflow.com\">Stackoverflow</a>");
$('body').append('<a href=\'http://stackoverflow.com\'>Stackoverflow</a>');

It's view result in body tag as

<a href="http://stackoverflow.com">Stackoverflow</a>
<a href='http://stackoverflow.com'>Stackoverflow</a>

Upvotes: 1

Sergii Stotskyi
Sergii Stotskyi

Reputation: 5390

$(function () {
     var parent = $('.ttl').first().parent();
     parent.append('<div class="ttl"><div class="ttlpadding"><div class="item">'
          + "<a href='" + data.data[i].images.standard_resolution.url
          +"' ><img src='"+ data.data[i].images.thumbnail.url +"' /></a>"
          +"</div></div></div>"
     );
     parent.find('a').click(function() {
        // some code here
     });
});

Upvotes: 1

Om3ga
Om3ga

Reputation: 32843

Is this what you want.

$('.ttl').first().parent().append('<div class="ttl"><div class="ttlpadding"><div class="item">' + "<a href='" + data.data[i].images.standard_resolution.url +"' onclick='panel_five.show();' ><img src='" + data.data[i].images.thumbnail.url +"' /></a>" +"</div></div></div>");

Upvotes: 1

Related Questions