I have the following data.
Two p tags with a name and an age. Then I have a list with certain values.
With jquery I get the name and the user without problem. But I don't know how to get all the values from the list, I guess I need a loop.
<p class="name"> User </p>
<p class="age"> 99 </p>
<ul>
<li> item1 </li>
<li> item2 </li>
<li> item3 </li>
<li> item4 </li>
</ul>
Once all the data is obtained, I would appreciate the methodology or what is used in js to create a json with said data
If you're using jQuery, you can use the .each() method . Precisely in their documentation, they put an example like the one you need:
With the selector
$("li")
you get an array with all the labels<li>
, and with the method.each()
you iterate over each of its elements.The anonymous function that you pass to the method
.each()
can have two parameters: index and element , so you could rewrite the above like this:... where index is the index of the array you are iterating over and element each of the
<li>
. You can use other properties of the element<li>
instead of innerHTML , such as innerText , outerText , or textContent .