data example

by jfriend00

HTML

<button id="go">Show Position</button><br /><br />

<ul id="main_list">
    <li>First</li>
    <li>Second</li>
    <li>Third</li>
    <li>Fourth</li>
</ul>

JavaScript

// put an attribute on each object that represents it's index so that's easier to get later
$("#main_list li").each(function(index){
    $(this).data("pos", index);
});

$("#go").click(function() {
    // now read each data element and show it's value
    $("#main_list li").each(function(index) {
        $(this).append(": pos=" + $(this).data("pos"));
    });
});