Reputation: 3495
I am using below Html code and I want to get all the <li>
tags from the <div id="divSelect">
and I want to get text from all li tags.
Please help , how to use .each()
and .find()
using JQuery.
Thanks
Upvotes: 0
Views: 9178
Reputation: 1934
Hey I have used your html and wrote a jQuery function which is using .each and .find to fetch all the li from the DIV.
we should use .find where we can use, its recommened by jQuery.(its performance is good if it is user wisely)
html code:-
<div id="divSelect" class="custom dropdown">
<ul>
<li>text 1</li>
<li>text 2</li>
<li>text 3</li>
</ul>
</div>
and javascript code is:-
$("#divSelect").find("li").each(function()
{
var $li=$(this);
alert($li.text())
});
thanks
Upvotes: 6
Reputation: 9637
Use map in jquery to collect all data ,this.outerHTML
in javascript to return the html element
var data=$("#divSelect ul li").map(function(){
return this.outerHTML;
}).get();
alert(data);
Upvotes: 0
Reputation: 56
$("#divSelect").find("li").each(function(){
alert($(this).html());
});
Upvotes: 0
Reputation: 1199
This can be achieved by
$("#divSelect ul li").each(function(index){
alert ( index + ": " + $( this ).text() );
});
Upvotes: 0
Reputation: 7207
try this:
var liText='';
$('#divSelect ul li').each(function(){
liText+=$(this).html();
});
liText will contain all the "li"s texts.
Upvotes: 0
Reputation: 33228
$("#divSelect > ul > li").text();
With this you get text from all li
elements.
Upvotes: 0
Reputation: 82241
To get them in array,You can use:
var alllitexts=$('#divSelect ul li').map(function(){
return $(this).html();
}).get();
using each and getting them individually:
$("#divSelect ul li").each(function(){
alert($(this).html());
});
Upvotes: 0