javascript - with - select name

How can I select an element by name with jQuery? (10)

function toggleByName() {
  var arrChkBox = document.getElementsByName("chName");
<script src=""></script>
    <input type="text" name="chName"/><br />
    <input type="text" name="chName"/><br />
    <input type="text" name="chName"/><br />
    <input type="text" name="chName"/><br />
    <input type="button" onclick="toggleByName();" value="toggle"/>

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?

    <td name="tcol1" class="bold"> data2</td>
    <td name="tcol1" class="bold"> data2</td>
    <td name="tcol1" class="bold"> data2</td>

Frameworks usually use bracket names in forms, like:

<input name=user[first_name] />

They can be accessed by:

// in JS:

// in jQuery:

// or by mask with escaped quotes:

Here's a simple solution: $('td[name=tcol1]')

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.

var tbl = $('#tableID');
var boldElements = $('.bold',tbl);
var rows = $('tr',tbl);
if (rows.length) {
   var row1 = rows[0]; 
   var firstRowCells = $('td',row1); 

You can get the element in JQuery by using its ID attribute like this:


You can use any attribute as selector with [attribute_name=value].


You can use the 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'

You forgot the second set of quotes, which makes the accepted answer incorrect:


<script src="jquery.min.js"></script>
<script type="text/javascript">
    var a= $("td[name=tcol3]").html();



<table border="3">
    <td name="tcol1" class="bold"> data2tcol1</td>
    <td name="tcol2" class="bold"> data2tcol2</td>
    <td name="tcol3" class="bold"> data2tcol3</td>

This is the Code which can be helpful.