Skip to content
Fix Code Error

How can I select an element by name with jQuery?

March 13, 2021 by Code Error
Posted By: Anonymous

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>

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'
Answered By: Anonymous

Related Articles

  • Is it possible to insert multiple rows at a time in an…
  • SQL query return data from multiple tables
  • vue-apexchart realtime chart is not smooth
  • data.table vs dplyr: can one do something well the other…
  • Hierarchical sidebarLayout() using the selectInput()…
  • update two remote api and retry multiple time
  • Best way to store data locally in .NET (C#)
  • How to prevent labels if height is not large enough to…
  • Intersect Two Lists in C#
  • Show/hide 'div' using JavaScript

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

What’s the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?

Next Post:

XAMPP, Apache – Error: Apache shutdown unexpectedly

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error