tags:

views:

62

answers:

5

Hi there

I am hiding a bunch of textboxes and it works fine, the problem is, the textboxes are in a table, so I also need to hide the corresponding labels. the structure is something like this

<tr>
<td>
Label
</td>
<td>
InputFile
</td>
</tr>

in fact its just easier if I hide the rows that have a fileinput , can someone help please

A: 

If the label is in a table row you can do this to hide the row:

('.InputFile').parent().Hide()

You can refine your selector as you need and then get the table row that contains that element.

JQuery Selectors help: http://api.jquery.com/category/selectors/

EDIT This is the correct way to do it.

    ('.InputFile').parents('tr').hide()
ICodeForCoffee
-1 The parent of the input control would be the `<td>`, and the hide method is lower-case
Josh Stodola
A: 
$('.trhideclass1').hide();

might work...

<tr class="trhideclass1">
<td>
Label
</td>
<td>
InputFile
</td>
</tr>
Derek
thanks for the down vote... I just don't see a point in targeting the parent elements when he could just attach a simple class to each row with a form field.
Derek
In my opinion, it's best to keep the markup as clean and concise as possible, both for maintenance and readability.
Bobby Jack
(Also, the intent of the question is surely 'how do I hide an element that contains a given element' not just 'how do I hide an element with a given class')
Bobby Jack
"in fact its just easier if I hide the rows that have a fileinput , can someone help please"so manually assign a class to each row with a 'fileinput', whatever that is.and in the current #1 answer they put an id instead of a class, I hope the asker knows he needs to change that.
Derek
Thanks Derek, your answer was the best solution in this case
tomasz
+1  A: 

You just need to traverse up the DOM tree to the nearest <tr> like so...

$("#ID_OF_ELEMENT").parents("tr").hide();

jQuery API Reference

Josh Stodola
I think that should be parents(), not parent()
Bobby Jack
Down vote -1. parent() takes the first element. parents() is the correct usage.
ICodeForCoffee
@Bobby @ICodeForCoffee You guys are right; I've updated my answer
Josh Stodola
+1  A: 

$('inputFile').parent().parent().children('td > label').hide();

can help you navigate two levels up ( to TD, to TR ) moving two levels back down ( all TD's in that TR and their LABEL tags ), applying the hide() function there.

if you want to stay at the TR level and hide them:

$('inputFile').parent().parent().hide();

… is sufficient.

you can navigate very easily through the elements using the jquery selectors.

parent is documented here: http://api.jquery.com/parent/

hide is documented here: http://api.jquery.com/hide/

favo
A: 

I think your best bet if you want both text field and label to hide simultaneously is assign each with a class and hide them like this:

jQuery(".labelClass, .inputClass").hide();
Joe D