user995195
user995195

Reputation: 15

how to get the value ot text node with jQuery

<div class="count">5</div>
<div class="count">6</div>

How do I get the values of the text nodes? If I use text() method, it concatenates both values.

Upvotes: 1

Views: 2528

Answers (4)

Wasim
Wasim

Reputation: 896

You can use this

$(".count:first").text()
$(".count:last").text()

Upvotes: 0

i100
i100

Reputation: 4666

what about

$("div.count:first").text();

if you have several and want to get 3rd for instance you could do

$("div.count:eq(2)").text();

Upvotes: 0

Ben
Ben

Reputation: 7597

You will need to loop the collection that results from $(".count"), and then grab the text() value of each in the loop.

Upvotes: 0

a&#39;r
a&#39;r

Reputation: 37029

Use map if you want an array of the text values, eg.

$('div.class').map(function() { return $(this).text(); })

Upvotes: 2

Related Questions