anjibcs
anjibcs

Reputation: 80

Setting focus on first td cell with input in table

I am having trouble setting focus on the first td cell with input box in the table. I change the cell content to input using solution from this post. Now I want to set focus on first input box. My table structure look like this

<table id="knowledgeTreeTable" class="custom">
  <tbody>
    <tr>
      <th class="">Who are the services being provided for?</th>
      <td class="">
        <input type="text" style="width: 97%;">
      </td>
    </tr>
    <tr>
     .....
    </tr>
    ..... 
   </tbody>
</table>

Upvotes: 0

Views: 4351

Answers (2)

nocarrier
nocarrier

Reputation: 129

This?

$(document).ready(function(){
    $("#knowledgeTreeTable tr:nth-of-type(1) input").focus();
});

CSS pseudo-class element:nth-of-type(n) selects the nth element of its type in the document. Elements separated by spaces mean a relation parent-child. So the code says something like

"Select an input whose parent is the 1st of its type and is child of an element with id knowledgeTreeTable"

About pseudo-classes

Upvotes: 0

Diego Montoya
Diego Montoya

Reputation: 139

Asumming you're using jquery, you could use something like this:

$('#knowledgeTreeTable input').first().focus();

Upvotes: 1

Related Questions