Hide field and text box label associated with it

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

In this case, I would like to be able to hide the text box and its label at a time, without having to select both elements and hide them separately.

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

So, for the above case, could one use a simple jQuery command to hide them together?

+5
source share
4 answers

You can do it

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

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

[ ]chooses attribute. In this case, we target an attribute forthat is equal a.

,, , input id=a.

, labels inputs, label id :

<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/

+13

, , :

function hideTandem(id){
    $('#' + id + ', label[for=' + id + ']').hide();
}

,

hideTandem('a');
+2

- div .

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

$("#hidethis).hide();
+1

, , ,

var myInputBoxID = $(some selector).attr("id");

, , :

$("label[for=' + myInputBoxID + ']").hide();

Perhaps you can invest in things well, so you don’t need to store anything as a variable and instead run “for each” across all the results of your input field selector. But in any case, the key finds the identifier, and then uses it to search for the tag with the tag [for = ...]

+1
source

All Articles