HTMLTableCellElement> has no method text in jquery

I am trying to extract the last 'tr' of a table and find out the value of the 3rd cell of this last tr. I use the following instruction

alert($("#requirements_table tr:last").find("td")[1].text()); 

but I get the error:

HTMLTableCellElement> has no method text

Table structure

 <table id="requirements_table"> <% @requirements.each do |requirement| %> <tr> <td><div contenteditable><%= requirement.id %></div></td> <td><%= requirement.requirement_info %></td> <td><%= link_to 'Show', requirement %></td> <td><%= link_to 'Edit', edit_requirement_path(requirement),:class=> "edit_req" %></td> <td><%= link_to 'Destroy', requirement, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </table> 
+4
source share
2 answers

Change

$("#requirements_table tr:last").find("td")[1].text()

to

$("#requirements_table tr:last").find("td").eq(1).text()

As the error .find("td")[1] indicates - returns a DOM element that does not have a .text() function

Instead, you need to use a .eq function, such as .find("td").eq(1).text() , which returns a jQuery object.

+5
source

The parenthesis designation for the jQuery object returns you the basic HTML element. To access the jQuery object at the specified index, you want to use eq () :

 $("#requirements_table tr:last").find("td").eq(1).text() 
+1
source

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


All Articles