...

JQuery selectors select the second cell of the second row of the 2nd form table with a specific action

<form action="blah"> <table>...</table> <table> <tr>...</tr> <tr> <td>...<td> <td> **some text that changes** </td> 

Given the above html structure, how do I get text that changes using jQuery

So, I want jquery to search as follows: for a form that has the action = "blah" attribute - select the second form table - select the second row (tr) of the table - select the second cell (td) of the row - give me the text

This will help me understand jquery ... I am new to this.

Something like $ ('form [action = "blah"]'). tables (2) .rows (2) .cells (2) .text, but this is not a valid jQuery match

+6
source share
3 answers

So, you want the second table cell in the second row of the table of the second table? eq function can do it easily

Demo

 var text = $("table:eq(1) tr:eq(1) td:eq(1)").text(); 

Also note that you did not close this table cell correctly:

 <td>...<td> 

it should be

 <td>...</td> 

EDIT

If there are other tables on the page and you want to make sure that you include the second form in the form, add the form to your selector:

 var text = $("form[action='blah'] table:eq(1) tr:eq(1) td:eq(1)").text(); 
+5
source

You can use .eq(index) .
To select the second item, use eq(1) .
JsFiddle works.

+2
source

Then select an object, then expand it. Adam had the right idea. You just want to avoid creating one giant selector. This is a compromise between readability and speed.

If you want to fully optimize your work, feel free to take each step down with find (). But if this selector is not reevaluated, it does not matter.

 $('form[action="blah"]').find("table:eq(1) tr:eq(1) td:eq(1)").text(); 

jsFiddle

+2
source

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


All Articles