josh asked this 3 years ago

jQuery loop through unordered list and access inner text

Hi,

I have span elements inside <li>, how can i access and change the text dynamically?

<ul id="listA">
<li><span></span></li>
<li><span></span></li>
<li><span></span></li>
<li><span></span></li>
</ul>

 


Best Answer by Vikas
6 likes

$("ul#listA li:nth-child(1) span").innerText()  gives you text inside the first li and span

dig_abacus 3 years ago
6 likes

Not sure if this is what you are looking for

list elements can be looped as

                       

var listItems = $("#listA li");

    listItems.each(function(li)

	{

	// do your stuff
        $(this).children('span').text("my content");

        });
josh 3 years ago
2 likes

How can i do this in a loop inside another function? 

I want the list to be accessed inside a function and then alter the contents dynamically.