views:

335

answers:

2

Hello!

I'm with Jquery.

I appended html to a div like that:

$('div#byletter').append(createLettersHtml());

The createLettersHtml function creates a list of letters (links) all wrapped in 'div.ln-letters'. Then I want to call a 'onclick' event like that:

$('.ln-letters a').click(function(){

    alert(123);

});

but nothing happens! If I will call onclick on any non-appended html element it works, but it doesn't work with appended content. What do I do wrong?

UPDATE:

Here is full code:

$(document).ready(function(){

$.fn.listnav = function(options) {
     var opts = $.extend({}, $.fn.listnav.defaults, options);
     var letters = ['_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '-'];
     var firstClick = false;
     opts.prefixes = $.map(opts.prefixes, function(n) { return n.toLowerCase(); });

$('div#byletter').append(createLettersHtml());   

function createLettersHtml() {
       var html = [];
       for (var i = 1; i < 27; i++) {
        if (html.length == 0) html.push('<a class="all" href="#">ALL</a><a class="_" href="#">0-9</a>');
        html.push('<a class="' + letters[i] + '" href="#">' + ((letters[i] == '-') ? '...' : letters[i].toUpperCase()) + '</a>');
       }
       return '<div class="ln-letters">' + html.join('') + '</div>' + ((opts.showCounts) ? '<div class="ln-letter-count" style="display:none; position:absolute; top:0; left:0; width:20px;">0</div>' : ''); // the styling for ln-letter-count is to give us a starting point for the element, which will be repositioned when made visible (ie, should not need to be styled by the user)
      }



    };

    $.fn.listnav.defaults = {
     initLetter: '',
     includeAll: true,
     incudeOther: false,
     includeNums: true,
     flagDisabled: true,
     noMatchText: 'No matching entries',
     showCounts: true,
     cookieName: null,
     onClick: null,
     prefixes: []
    };


    $('.ln-letters a').click(function(){

     alert(123);

});

});
A: 

You have to set click evet AFTER you append HTML content. Are you sure you are doing that? Besides, use Firebug or Opera Dragonfly to check if you have any errors in you JS.

usoban
I'm a novice in js, so I actually don't understand what do you mean by AFTER. Well, yes, it goes after this append string: $('div#byletter').append(createLettersHtml());
Andersson83
firebug shows no errors btw
Andersson83
After means, that you append the content first and then set click event. If you do that before, element doesn't exist and the script breaks.
usoban
Please see a full code in the update to this question
Andersson83
usoban's hypothesis is right: you add the handler before the nodes are appended. Nodes are appended AFTER the call to createLettersHtml has returned, while you set the click event IN that function. You need to move it to the line after the append call.
Johannes Hoff
+4  A: 

you can try using .live. You can hook this up in doc ready. .live works well for appended content.

$(function(){
   $('div.ln-letters a').live('click', function(){
      alert(123);
   });
});
redsquare
with .live event content doesn't appear at all
Andersson83
not sure what you mean. Hooking an event up should not matter to content appending. Can you post the the createLettersHtml function so we can get a better idea of what your trying to do.
redsquare
Sure, I added this as an update to this question
Andersson83
same applies, using live should work just fine
redsquare
@Andersson83 - redsquare is right, this is the way to go about it. Also, make sure you are using jQuery 1.3 or greater as that is when live was added.
karim79
Well, yes, the reason was I had jquery older than 1.3, with 1.3 now it works, but excuse me, the size of minified jquery 1.3 is 55KB against mine packed 29KB... well I think twice before moving to 1.3. I guess there are no ways to do it without live()?
Andersson83
@Andersson83 - I would advise you to switch regardless of the added KBs. Most browsers will cache that anyway, if you haven't set your own cache-expiry and content-not-modified headers. Just think of all the extra stuff you get in those extra bytes!
karim79
also minifies + gzip is the way to go. This easily outways the overhead of the client having to un-pack.
redsquare
@karim - good spot re the >1.3. Should be mandatory to specify the version in a question!
redsquare