Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Textbox Field and Label associated with it

Tags:

html

jquery

This is probably a pretty pointless question but I reckon it would be useful for many of us.

In this case's scenario, I would like to be able to hide a textbox and its label all in one go, without having to select both element and hiding them individually.

<label for="a">some text:</label>
<input type="text" class="text" id="a" />

So, for the above case, would it be possible to use a simple jQuery command to hide them both together?

like image 438
Hallaghan Avatar asked Jul 29 '11 16:07

Hallaghan


People also ask

How to hide label and TextBox in html?

HTML input type="hidden"


1 Answers

You could do this

$('label[for=a], input#a').hide();

http://jsfiddle.net/jasongennaro/dYFMU/

[ ] selects the attribute. In this case we are targeting a for attribute that equals a.

At the same time, we use a , to make another selection, the input with an id=a.

EDIT

Also, if you needed to do this for multiple labels and inputs you could put the label and id in an array as follows:

<label for="a">some text:</label>
<input type="text" class="text" id="a" />

<br />

<label for="b">some text:</label>
<input type="text" class="text" id="b" />

<br />

<label for="c">some text:</label>
<input type="text" class="text" id="c" />

js

var a = ['a','b','c'];

for(var i=0; i<a.length; i++){
    $('label[for=' + a[i] + '], input#' + a[i]).hide();
}

http://jsfiddle.net/jasongennaro/dYFMU/1/

like image 149
Jason Gennaro Avatar answered Sep 19 '22 12:09

Jason Gennaro