Domanda

I have following html:

<table>
  ....
  <tr>
    ....
  </tr> 
  ....
  <tr id="myid">
     <....>
        <input value="..." />
     </....>
  </tr>
  <tr id="myanotherid">
     <....>
        <input value="..." />
     </....>
  </tr>
</table>

How can I set the focus on this input field in table row "myid" with jquery ? I can't set id on input field, because this html was generated automatically.

È stato utile?

Soluzione

Use the table as an anchor and find the input regardless of its value.

$('table tr#myid').find('input:first').focus()

Altri suggerimenti

Selecting the field as-is:

$('input[value="abc"]').focus();

Or if there's a chance that there will be more than one input with that value:

$('input[value="abc"]:first').focus();

If it's the ID you need to focus on, use that in your selector:

$('#myid input:first').focus();

more jQuery selectors

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top