Tuesday, 18 July 2017

javascript - How can I select an element by name with jQuery?



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?




    
data1
data2


data1
data2



data1
data2


Answer



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'

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...