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 element's name.
For example, why does:
$(".bold").hide(); // selecting by class works
$("tcol1").hide(); // select by element name does not work
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>
Frameworks usually use bracket names in forms, like:
They can be accessed by:
Any attribute can be selected using
[attribute_name=value]
way. See the sample here:This is the Code which can be helpful.
You forgot the second set of quotes, which makes the accepted answer incorrect:
If you have something like:
You can read all like this:
The snippet:
Personally, what I've done in the past is give them a common class id and used that to select them. It may not be ideal as they have a class specified that may not exist, but it makes the selection a hell of a lot easier. Just make sure you're unique in your classnames.
i.e. for the example above I'd use your selection by class. Better still would be to change the class name from bold to 'tcol1', so you don't get any accidental inclusions into the jQuery results. If bold does actually refer to a CSS class, you can always specify both in the class property - i.e. 'class="tcol1 bold"'.
In summary, if you can't select by Name, either use a complicated jQuery selector and accept any related performance hit or use Class selectors.
You can always limit the jQuery scope by including the table name i.e. $('#tableID > .bold')
That should restrict jQuery from searching the "world".
Its could still be classed as a complicated selector, but it quickly constrains any searching to within the table with the ID of '#tableID', so keeps the processing to a minimum.
An alternative of this if you're looking for more than 1 element within #table1 would be to look this up separately and then pass it to jQuery as this limits the scope, but saves a bit of processing to look it up each time.