0

I have a javascript code that have span tag and inside the span tag there is an image tag and normal text next to the image tag. I want to bind the click event on image tag but the event didn't fire at all. I use the jquery to accomplish this.

var a = new Image();
$(a).prop('src','blah');
$(a).load( blah);


$(span).append($(a));
$(a).click(function (e) { alert('asdf'); } );

not event if i do this

$(span).find($(a)).click( blah );


 var button = $(this.imgClose).clone();
        $(button).prop('id', 'close');
        $(template).append($(button));

        $(template).find('#close').on('click', '', function (event) { alert('sss'); });
LittleFunny
  • 8,155
  • 15
  • 87
  • 198
  • Hiya @AndrewWhitaker if you will set it as answer I will upvote you bruv, cheers! – Tats_innit Jun 26 '12 at 00:04
  • Thank to you all.. it's finally working ... I don't know why I add the event after the line : var value = $(template).html() + text; $(template).html(value); – LittleFunny Jun 26 '12 at 00:21

2 Answers2

1
$(span).append($(a));

Should be:

$('span').append($(a));

Live DEMO

gdoron
  • 147,333
  • 58
  • 291
  • 367
1

Please try this since you are appending the a and then trying to click

Rest you can see @gdoron 's & @Andrew stuff above as well

API reside here: http://api.jquery.com/on/

Also prop vs attr : .prop() vs .attr()

hope this helps,

code

$('a').on('click', function(){ 
   alert('HULK');
});
Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • i use on as well but still no effect. But if I add the image tag outside of a span tag it clickable – LittleFunny Jun 26 '12 at 00:14
  • Hey @Simon try this `$('span').siblings('a').click(function() {..` (API: http://api.jquery.com/siblings/) **OR** flick me your code in jsfiddle I will help you out bruv, cheers! – Tats_innit Jun 26 '12 at 00:16