Skip to content Skip to sidebar Skip to footer

Add A Different Id To Each Li Element By Jquery

I'm new here and I'd like to ask a question about jQuery. I have the unordered list like:
  • Solution 1:

    As of jQuery 1.4, you can do this:

    $('#pages li').attr('id', function(i) {
       return'page'+(i+1);
    });
    

    In earlier versions, you'd need to write:

    $('#pages li').each(function(i) {
        $(this).attr('id', 'page'+(i+1));
    });
    

    ... which works in 1.4 as well. It's a matter of preference, I guess.

Post a Comment for "Add A Different Id To Each Li Element By Jquery"