Roby
Roby

Reputation: 2061

jquery get Element Text

i have a small list

like

<div class="d1">
<ul>
<li class="active">hello</li>
<li>foo</li>
</ul>
</div>

<div class="d2">
<ul>
<li class="active">foo</li>
<li>foo</li>
</ul>
</div>

<div class="d3">
<ul>
<li class="active">bar</li>
<li>foo</li>
</ul>
</div>

and now i want to get an array with , in this example the size 2 [0] -> hello [1] -> foo

but i only want to get all 'active' marked values from div d1, d2 . but coult not find how to access the value.

I think it must be somethink like

$('#d1,#d2 li').value

but it didnt work :-/

Upvotes: 0

Views: 3533

Answers (4)

Zoltan Toth
Zoltan Toth

Reputation: 47687

Try this

var arr = new Array();

$(".d1, .d2").find("li.active").map(function() {
    arr.push( $(this).text() ); 
});

DEMO

Upvotes: 1

arulmr
arulmr

Reputation: 8846

Use this

var array = new Array();
$('.d1 li.active, .d2 li.active').each(function(){
    array.push($(this).text())
});

Upvotes: 1

xdazz
xdazz

Reputation: 160923

d1 and d2 is class, so you should use $('.d1,.d2'), then you find the active li, use map to return all the text value of the found li.

var ret = $('.d1,.d2').find('li.active').map(function() {
  return $(this).text();
});

Upvotes: 0

Andreas
Andreas

Reputation: 21911

$(".d1, .d2").find("li.active").map(function(i, e) { return $(e).text(); })

Upvotes: 3

Related Questions