JQuery: value () and text () do not return table cell text

I have to miss something basic here. Enlighten me.

I am trying to capture the id (289 in this example):

<tr class="header"> <th>ID</th> <th>2</th> <th>3</th> </tr> <tr class="highlight"> <td class="">289</td> <td class="">field a</td> <td class="">field b</td> </tr> ... more rows 

I use this selector:

 $("#requests :nth-child(2) td:first") 

The Firebug console shows this:

 Object { length=1, more...} 

Forward. Grabbing the first element of this:

 >>> $("#requests :nth-child(2) td:first")[0] <td class=""> 

So, I thought I could call text() or value() or some such method.

If I look at the DOM tab in Firebug, I see that I have "childNodes" and "firstChild" with <TextNode textContent="289"> , but I cannot figure out how to get this.

+4
source share
3 answers

You need to write $("#requests :nth-child(2) td:first").text() .

Using the jQuery object indexer ( $(...)[0] ) will return the raw DOM element.
If you want to call jQuery methods, you need to call them directly on the jQuery object without using an indexer.
If you want to call the jQuery method for a single element in the jQuery set, call eq , for example: $(...).eq(3).text() .

There is no value() method in jQuery.
The val method sets or returns the value of a form element.

+9
source

When you use a brace note to grab an object from a jQuery object, it grabs the original dom element. Use $('selector').eq(0) instead. Then your methods should work.

+2
source

.text() returns all the text inside the element, so it should work.

Seeing no more of your HTML, there is no way to know if you have

 $("#requests :nth-child(2) td:first") 

matches the corresponding element

0
source

Source: https://habr.com/ru/post/1302256/


All Articles