DotnetSparrow
DotnetSparrow

Reputation: 27996

getting first div contents using jquery

I am using following code to get columns of rows:

$.each( items, function(i, item){
     columns = $(item).children();
     //columns[5]
     //columns[7]
});

now each column has a div. I want to get inner contents of div. I tried to use columns[5] and it gave div like this

<div style= "" > 123 </div>

I need 123 only

Upvotes: 0

Views: 94

Answers (3)

Nope
Nope

Reputation: 22329

Seeing columns is a jQuery collection you can use eq() to get the jQuery object referencing the DOM element in the specified index. As it is a jQuery object which is returned by eq() you can call jQuery's text() method on it.

$.each( items, function(i, item){
    columns = $(item).children();
    columns.eq(5).text();
});

Alternatively you can get the actual DOM element from the jQuery collection at the specified index and call innerHtml directly:

$.each( items, function(i, item){
    columns = $(item).children();
    columns[5].innerHtml;
});

Upvotes: 0

James Montagne
James Montagne

Reputation: 78630

columns.eq(5).find("div:first").text();

Upvotes: 0

Elliot Bonneville
Elliot Bonneville

Reputation: 53291

You'll want .text() for this.

$.each( items, function(i, item){
     columns = $(item).children();
     alert($(columns[5].text())); // create jQuery object and get text from that
});

Upvotes: 1

Related Questions