I'm trying to add the class name available in the th
tag to all dropdowns inside the corresponding th.
For example:
The second th has the class name 6. So need to add 6 as class name to all the dropdowns available in the second td's in the rows. I've tried the answer mentioned in this post: JQuery: If a table header <th> has a class, add class to table cell <td>. But it's working fine in my case.
Thanks in advance.
$('tbody tr td .get_hnos').each(function(index){
debugger;
//index contains the current td index in the parent tr (1,2,3),(1,2,3)...
//if the corresponding th has a class
if($('thead tr th').eq(index).attr('class') != ''){
//put this class on the current td
$(this).addClass($('thead tr th').eq(index).attr('class'));
}
});
<table class="table table-responsive table-bordered">
<thead>
<tr>
<th>Sr.</th>
<th class="6">Title 1</th>
<th class="7">Title 2</th>
</tr>
</thead>
<tbody><tr> </tr><tr><input type="hidden" name="sr_no" value="30"><input type="hidden" name="batch" value=""><input type="hidden" name="valve_no[]" value="1"><td>1</td><td><select name="1[]" class="get_hnos" id="1"><option value="BH123" selected="">BH123</option>
</select>
</td>
<td><select name="1[]" class="get_hnos 6" id="1">
<option value="H89" selected="">H89</option>
</select>
</td>
</tr>
<tr> </tr><tr><input type="hidden" name="sr_no" value="30"><input type="hidden" name="batch" value=""><input type="hidden" name="valve_no[]" value="2"><td>2</td><td><select name="2[]" class="get_hnos 7" id="2"><option value="BH123" selected="">BH123</option>
</select>
</td>
<td><select name="2[]" class="get_hnos" id="2">
<option value="H89" selected="">H89</option>
</select>
</td>
</tr>
<tr> </tr><tr><input type="hidden" name="sr_no" value="30"><input type="hidden" name="batch" value=""><input type="hidden" name="valve_no[]" value="3"><td>3</td><td><select name="3[]" class="get_hnos" id="3"><option value="BH123" selected="">BH123</option>
</select>
</td>
<td><select name="3[]" class="get_hnos" id="3">
<option value="H89" selected="">H89</option>
</select>
</td>
</tr>
<tr> </tr><tr><input type="hidden" name="sr_no" value="30"><input type="hidden" name="batch" value=""><input type="hidden" name="valve_no[]" value="4"><td>4</td><td><select name="4[]" class="get_hnos" id="4"><option value="BH123" selected="">BH123</option>
</select>
</td>
<td><select name="4[]" class="get_hnos" id="4">
<option value="" selected=""></option>
</select>
</td>
</tr>
</tbody></table>