Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select an element by name with jQuery?

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. 

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> 
like image 394
T.T.T. Avatar asked Jul 10 '09 01:07

T.T.T.


People also ask

How does jQuery select element?

jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors. All selectors in jQuery start with the dollar sign and parentheses: $().

How do you select element by id in jQuery?

getElementById() which is used to select the element by its id attribute. The getElementById() method returns the elements that has given ID which is passed to the function. This function is widely used in web designing to change the value of any particular element or get a particular element.

How do you find the value of an element with a name instead of ID?

Just type the name of the element without "<" and ">" characters. For example type P, not <P> if the answer is the <P> element.

Does jQuery use CSS selectors to select elements?

jQuery uses CSS selector to select elements using CSS. Let us see an example to return a style property on the first matched element. The css( name ) method returns a style property on the first matched element. name − The name of the property to access.


1 Answers

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' 
like image 91
Jon Erickson Avatar answered Nov 18 '22 21:11

Jon Erickson