JavaScript is there any easier way to get the LI list number in one UL?

i have following html code:

<ul>
    <li>Text</li>  <!-- number:0; means first one-->
    <li>Text</li>  <!-- number:1; second one-->
    <li>Text</li>  <!-- number:2; third one-->
    <li>Text</li>  <!-- number:3 -->
    <li>Text</li>  <!-- number:4 -->
    <li>Text</li>  <!-- number:5 -->
</ul>

in my method, set a attribute number to every li

i = 0;
$('li').each(function()
    {
        $(this).attr('number',i);
        i++;
    });

using $(this).attr('number') to get its number;

here is jsFiddle DEMO http://jsfiddle.net/8tpab84s/, is working

but i think the method is complicated, if the DOM li be changed, i need to set the number attribute again.

is there a easier way to instead this method ? without set any attribute.

thanks for your response.

Answer:1

I'm sending images/file within form data to a rest api and i was able to send the data using code like this before when the user had selected a file: var data = new FormData(); var files = $("#...

I'm sending images/file within form data to a rest api and i was able to send the data using code like this before when the user had selected a file: var data = new FormData(); var files = $("#...

Trying to use a form box with HTML to create a url that can be used to change an IFRAME. This is what I currently have. <input type="url" id="URL" value=""> <button onclick="webFunct()">...

Trying to use a form box with HTML to create a url that can be used to change an IFRAME. This is what I currently have. <input type="url" id="URL" value=""> <button onclick="webFunct()">...

  1. display form from excel
  2. display data from form javascript
  3. display data from form html
  4. display input from form
  5. display data from form php
  6. display image from form
  7. display form
  8. display form inline
  9. display form on button click
  10. display form on button click in html
  11. display form data on html page
  12. display form errors django
  13. display form powerapps
  14. display form sap
  15. display form results with javascript
  16. display form html
  17. display form input on same page
  18. display form data before submit
  19. display form validation errors in codeigniter
  20. display form elements inline

I have a jointjs demo code, which has basic shapes on paper, i want to increase the size of the shape, or highlight the shape on pointer click or cursor moved on the shape, sample demo code goes here ...

I have a jointjs demo code, which has basic shapes on paper, i want to increase the size of the shape, or highlight the shape on pointer click or cursor moved on the shape, sample demo code goes here ...

Let's say I have a row of divs like here: jsfiddle.net/AlphaCrack/veuc80he/1/ I wan't to animate them separately but keeping the same id because I may have to create tons of divs like those and I don'...

Let's say I have a row of divs like here: jsfiddle.net/AlphaCrack/veuc80he/1/ I wan't to animate them separately but keeping the same id because I may have to create tons of divs like those and I don'...

  1. apply same company different job
  2. apply same job different recruiters
  3. apply same css to different classes
  4. apply same formula to different cells
  5. apply same formula to different rows