1

I am trying to add spans to a div via jquery. These spans should have an onclick event. The spans seem to be added, but the newly created elements' click events don't fire. I'm worthless at jQuery and am not sure what I'm missing. Any help is appreciated.

Here is some relevant code:

<script type='text/javascript'>
$(window).load(function(){
     $('#Match .WordSpan').click(function() {
          alert($(this).html());
          $('#Match').append('<span>me too!</span><br/>').html();
          $("span:last").addClass("WordSpan").html();
     });
});
</script>

<input type="text" id="WordInput"  />
<div id="Match">
     <span class="WordSpan">click me!</span><br />
</div>

I have it in a fiddle here.

What I am hoping the code will do is, after clicking on the hard coded "click me" span, another span will be added below. When clicked, that span will add another, saecula saeculorum.

Where have I blown it?

1
  • 1
    A really tiny thing. But you could clean up your code by using: $('#Match').append('<span class="WordSpan">me too!</span><br/>'); Saving traveling in the DOM. Maybe even using $(this).after('Your new span here'); save you even more power, but I'm not too sure about that. Commented Dec 1, 2011 at 17:44

4 Answers 4

2

Using delegate performs better than using live:

$('#Match').delegate('span.WordSpan', 'click', function() {
    alert($(this).html());
    $('#Match').append('<span>me too!</span><br/>');
    $("span:last").addClass("WordSpan");
});

If using jquery 1.7+ then use on instead of delegate. jsFiddle example

Sign up to request clarification or add additional context in comments.

1 Comment

3 great answers straight away, but I'm giving you the prize because, according the jQuery docs on ".live", it is deprecated in 1.7 and ".delegate" should be used in older versions. Thanks a lot!
2

Try this:

$('#Match .WordSpan').live('click', function()....

Fiddle: http://jsfiddle.net/maniator/kLJ3J/3/

I also removed those random html()'s at the ends. not sure why you had those there...

2 Comments

As per the small-type comment, the .html() didn't make sense. Also, since .live() has been deprecated in jQuery 1.7+, here's Neal's code modified to use jQuery 1.7.1 and the .on() function: jsfiddle.net/kLJ3J/8
I'm not sure why I did either... just throwing code at the problem, hoping it would go away, I guess..
2

Starting in jQuery 1.7, do this:

 $('#Match').on('click','.WordSpan',function() {
      alert($(this).html());
      $(this).append('<span class="WordSpan">me too!</span><br/>');
 });

before 1.7 (but on or after 1.4.2 as noted by @Greg Pettit) do this:

 $('#Match').delegate('.WordSpan','click',function() {
      alert($(this).html());
      $(this).append('<span class="WordSpan">me too!</span><br/>');
 });

Also, instead of using a <br>, perhaps you could take care of that in your CSS, or by using an element other than span.

If you do that, you could use this form of creating elements:

$('<span>',{className:'WordSpan',text:'me too!'}).appendTo(this);

jQuery has built in optimizations for creating elements this way.

1 Comment

before 1.7 but after... 1.4? .delegate() had a relatively short lifespan.
1

Use live:

$('#Match .WordSpan').live("click", function() {
    $('#Match').append('<span class="WordSpan">me too!</span><br/>');
    $("span:last").addClass("WordSpan");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.