views:

639

answers:

2

There is unordered list, which not works properly in IE6-7, I would like want to replace it by a jQuery function.

So, we have:

  • simple list with special #id (ul id="")
  • some <li> tags
  • some special tag in <li> to show the number of a row (or something else, as you wish).

I need a function which will give an unique number for each row, from the beginning to the end of our unordered list. (ul)

A: 

I think this should work, no?

$(function() {
    $('#the_ul_ID li').each(function(i) { $(this).attr('rel',++i); });
});
KyleFarris
KyleFarris, thanks for answer. // this function adds rel="" for a <li> // The numbers need to be visible, how can I do that? // maybe add them in some tag and place inside <li>
Mike
+2  A: 
<ul><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li></ul>

$(document).ready(function(){   
    var increment=3;
    var start=8;
    $("ul").children().each(function(i) {
     $(this).prepend('<tag>'+(start+i*increment).toString()+'.</tag>');
    });
});

result:

* 8. test
* 11. test
* 14. test
* 17. test
* 20. test
* 23. test
* 26. test

edit: without increment and shorter:

$(document).ready(function(){   
    $("ul").children().each(function(i) {
     $(this).prepend('<b>'+(1+i).toString()+'.</b> ');
    });
});
zalew
veery good, how to place a number inside some tag? :)
Mike
$(this).prepend('<tag>'start+i*increment+'.</tag>');saw your deleted post on the increment felxibility, don't see this point in this question though ;)
zalew
$(this).prepend('<tag>'start+i*increment+'.</tag>'); - craches and show nothing
Mike
you forgot to add the plus - "'<tag>'+start" :)
Mike
need to start the list from one (1), changed increment and start for = 1, but it starts from 10 - how can I fix that?
Mike
fixed the + tag. 10? I see 1 as it should be.
zalew
It doesnt work when I add some tag inside, "$(this).prepend('<tag>'start+i*increment+'.</tag>')" - this also doesnt work, "$(this).prepend('<tag>'+start+i*increment+'.</tag>')" - this works but starts from 10, needed to start from 1.
Mike
dont sleep please(
Mike
oh, ok, i see. one sec.
zalew
see the edited version
zalew
that works now.can you make it shorter? there is no need for start and increment, the number will go from 1 to XXXXXXXXXXX.
Mike
like this - http://stackoverflow.com/questions/812875/generating-an-unordered-list-with-jquery-80-ready/812890#812890
Mike
but dont remove a ".children()", another way script will be broken if there is a one more list inside <li>
Mike
inside some <li>*
Mike
then green is better, isnt it? :)
Mike
with green is always better ;) shorter version in the edit. cheerz
zalew
Are you trying to get your homework done by somebody else?
dalbaeb
I'm waiting, and eating my boots in nervous
Mike
>edit: without increment and shorter:- THIS WORKS!!! YEEEEHA!11 THANKS FOR YOU, JZ! THIS SITE ROCKS!!
Mike
plused your post
Mike
>get your homework done - go a hell, small russian pig))
Mike
JZ RULES< HE IS THE GOD OF JAVASCRIPT, call me, I will by you a bear
Mike
*beer, buy* :))
Mike
thanks again, you saved my ass
Mike