I have a table column I’m trying to expand and hide. jQuery seems to hide the <td>
elements when I select it by class
but not by the element’s name
.
For example:
$(".bold").hide(); // Selecting by class works.
$("tcol1").hide(); // Selecting by name does not work. [Edit: this is NOT selecting by name! This is selecting by element type.]
Note the HTML below. The second column has the same name
for all rows. How could I create this collection using the name
attribute?
<tr>
<td>data1</td>
<td name="tcol1" class="bold"> data2</td>
</tr>
<tr>
<td>data1</td>
<td name="tcol1" class="bold"> data2</td>
</tr>
<tr>
<td>data1</td>
<td name="tcol1" class="bold"> data2</td>
</tr>
You can use the jQuery attribute selector:
$('td[name="tcol1"]') // Matches exactly 'tcol1'
$('td[name^="tcol"]' ) // Matches those that begin with 'tcol'
$('td[name$="tcol"]' ) // Matches those that end with 'tcol'
$('td[name*="tcol"]' ) // Matches those that contain 'tcol'