snippetjavascriptCritical
How can I select an element by name with jQuery?
Viewed 0 times
withhowjquerynameelementcanselect
Problem
I have a table column I’m trying to expand and hide. jQuery seems to hide the `
$("tcol1").hide(); // Selecting by name does not work. [Edit: this is NOT selecting by name! This is selecting by element type.]
data1
data2
data1
data2
data1
data2
`
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?
data1
data2
data1
data2
data1
data2
`
Solution
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'
Context
Stack Overflow Q#1107220, score: 2777
Revisions (0)
No revisions yet.