views:

11449

answers:

4

I have a table column I am trying to expand and hide. jQuery seems to hide the column when I select it by class but not by element name.

Why does

$(".bold").hide();  <--This work
$("tcol1").hide(); <--This NOT work??

The second column has the same name and id for all rows:.

<tr>    
    <td>data1</td>
    <td name="tcol1" id="tcol1" class="bold"> data2</td>
</tr>
<tr>    
    <td>data1</td>
    <td name="tcol1" id="tcol1" class="bold"> data2</td>
</tr>  
<tr>    
    <td>data1</td>
    <td name="tcol1" id="tcol1" class="bold"> data2</td>
</tr>
A: 

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

$("#tcol1").hide();
CalebHC
Matches a single element only
Tommy
Sorry about that. Just do what Ben S said.
CalebHC
Yes that is true about the ID. As far as selecting by name, I think there was a post up here referring to Selectors/attributeEquals which was helpful.
Tommy
+11  A: 

$("#tcol1").hide(); looks for the element with ID tcol1 to hide.

Your IDs must be unique. When more than one element has the same ID behavior is undefined since the HTML is malformed.

Many elements can share the same class, which is why $(".bold").hide(); works.

If you know that you want to get the second <td> in each <tr> you could omit IDs, names and classes and use $("tr td:nth-child(2)") to select that. This solution would reduce the size of your markup.

Ben S
Good point. I missed that. :p
CalebHC
you missed the #
redsquare
I added a way to do this without need for IDs, classes or names.
Ben S
amended to show the #
redsquare
+24  A: 

you can use the attribute selector

$('td[name=tcol1]') // matches exactly 'tcol1'

$('td[name^=tcol]') // matches those that begin with 'tcol'
Jon Erickson
Thanks. .........
Tommy
A: 

You could get the array of elements by name the old fashioned way and pass that array to jQuery.

<html>
<head>
    <title>sandBox</title>
</head>
<body>
    <input type="radio" name="chName"/><br />
    <input type="radio" name="chName"/><br />
    <input type="radio" name="chName"/><br />
    <input type="radio" name="chName"/><br />
    <input type="button" onclick="toggleByName();" value="toggle"/>
</body>
</html>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
    function toggleByName() {
        var arrChkBox = document.getElementsByName("chName");
        $(arrChkBox).toggle();
    }
</script>

note: the only time you would have a reason to use the "name" attribute should be for checkbox or radio inputs.

Or you could just add another class to the elements for selection.(This is what I would do)

<html>
<head>
    <title>sandBox</title>
</head>
<body>
    <table>
        <tr>
            <td>data1</td>
            <td class="bold rowToToggle">data2</td>
        </tr>
        <tr>
            <td>data1</td>
            <td class="bold rowToToggle">data2</td>
        </tr>
        <tr>
            <td>data1</td>
            <td class="bold rowToToggle">data2</td>
        </tr>
    </table>
<input type="button" onclick="toggleByClass(true);" value="show"/>
<input type="button" onclick="toggleByClass(false);" value="hide"/>
</body>
</html>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
    function toggleByClass(bolShow) {
        if (bolShow) {
            $(".rowToToggle").show();
        } else {
            $(".rowToToggle").hide();
        }
    }
</script>
x13