How to select a classes with same name without using incremental id

120

Question: How to select a classes with same name without using incremental id

enter image description hereIs there other ways to select classes with same name but in different table rows? I use html class="className + id" Than in jquery to select $('.className'+id)... some code I would like to avoid using the id each time. Some suggestions? Thank you

HMTL Data retrieved from the database <tr class="row" id="<?= $id;?>"> <!-- id 1 -->    <td  class="atyp<?= $id;?>"> <?= $v['atyp'];?> </td>   <tr class="row" id="<?= $id;?>"> <!-- id 2 -->    <td  class="atyp<?= $id;?>"> <?= $v['atyp'];?> </td>  JQUERY <script> $('.atyp'+id).val() // some code.... </script> 

So the table has multiple rows with same class name and I would like to php error avoid to use id to select a specific class

Total Answers: 2

55

Answers 1: of How to select a classes with same name without using incremental id

Don't put <?= $id;?> to your classes:

<tr class="row" id="<?= $id;?>"> <!-- id 1 -->    <td  class="atyp atyp<?= $id;?>"> <?= $v['atyp'];?> </td>   <tr class="row" id="<?= $id;?>"> <!-- id 2 -->    <td  class="atyp atyp<?= $id;?>"> <?= $v['atyp'];?> </td> 
$('.atyp').val() 
66

Answers 2: of How to select a classes with same name without using incremental id

You should not give multiple HTML elements the same ID.

You can select the first td with the :first-of-type selector

$("td:first-of-type") 

In general you can use the :nth-of-type selector.

$("td:nth-of-type(42)")