treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Element number (jQuery)

  • Hello :) I hope you guys can help me with my little problem. Is it possible with jQuery to add "This like li number x" to the li?

    Let's say I have this list

    <ul>
    <li></li>
    <li></li>
    <li></li>
    </ul>


    What I want it to do is saying something like this:


    <ul>
    <li>This is li number 1</li>
    <li>This is li number 2</li>
    <li>This is li number 3</li>
    </ul>


    I have searched in a long time and I havn't found anything yet, so I hope you guys can help me. If you need more details or something, just write ;)

    Thanks!

    Here's an illustration of it
    http://img27.imageshack.us/img27/9413/unavngivetu.png
  • we need more details. Like why you would want that. Why not just use html to number them? Like an ordered list?

    <ol>
    <li></li>
    <li></li>
    </ol>


    that would give you numbered li's
  • Ok, I've tried to illustrate what it hopefully will end with.

    http://img27.imageshack.us/img27/9413/unavngivetu.png

    Hope it makes more sense
  • You can loop through them with jQuery like

    $(\"li\").each(function(i) {

    $(this).append(\"<span>\" + i + \"</span>\");

    });


    That will append a span of the number of each list element. Hopefully that'll work anyway...
  • Thanks! :D :D It works! But is there a way it can start on 1 instead of 0? Is it just best to add an empty li?