简体   繁体   中英

How to access td from tr with id Jquery

My tr has id and two columns(tds) I want to change the second td html dinamically by doing this:

$("#value_"+ control_id+":nth-child(2)").html(type_id_name);

but it wipes all tds and it inserts the html in on column.

this is the table structure

<tr id="value_486">
  <td></td>
  <td></td>
<tr>

I tried different approaches as

$("#tableID tr#value_"+ control_id+":nth-child(2)").html(type_id_name);

but it doesnt work either.

You need to give a space or a td before the :nth-child . Space denotes a child or descendant selector:

$("#tableID tr#value_"+ control_id+" :nth-child(2)").html(type_id_name);
$("#tableID tr#value_"+ control_id+" td:nth-child(2)").html(type_id_name);

Your current one without the space:

$("#tableID tr#value_"+ control_id+":nth-child(2)").html(type_id_name);

Selects the second instance of the <tr> , which is not there!

 $(function () { var control_id = 486; var type_id_name = "Praveen"; $("#tableID tr#value_"+ control_id+" :nth-child(2)").html(type_id_name); $("#tableID tr#value_"+ control_id+" td:nth-child(2)").html(type_id_name); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tableID"> <tr id="value_486"> <td></td> <td></td> <tr> </table> 

And yeah, you don't need two #id selectors, when you are using #id to select. Your first style works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM