Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a label tag in jQuery that has a certain value for the "for" attribute?

Tags:

jquery

I want to hide this label tag:

<label for="id_url">URL</label>

Only thing that distinguishes it from the other form elements is the value inside the for attribute. I don't have access to the form's html to add a class or id to it. So how can I hide it?

like image 689
Sam Avatar asked Jun 20 '11 15:06

Sam


People also ask

How can make label invisible in jQuery?

Use jQuery . hide()[^] function. $('#lblId'). hide(); // Replace the correct ID of Label.

How do I hide label tags?

The hidden attribute hides the <label> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <label> element is not visible, but it maintains its position on the page.

What is hide () in jQuery?

The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.

How do I make label visible true in jQuery?

You can then use the jQuery hide() and show() functions. Show activity on this post. Set the CSS property visibility to visible . Show activity on this post.


2 Answers

Use the attribute-equals selector.

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

If you need to use a separate variable that contains the target attribute value:

var target = 'id_url'; // or whatever
$('label[for="' + target + '"]').hide();
like image 175
Matt Ball Avatar answered Sep 25 '22 20:09

Matt Ball


The attribute Equals selector will match exactly the value of any attribute.

$('label[for="id_url"]').hide()

http://api.jquery.com/attribute-equals-selector/

You can also use other selectors (starts with, ends with, etc.) if you have many labels and/or can't necessarily search for a perfect match.

http://api.jquery.com/category/selectors/

EDIT: note that, from a performance standpoint, attribute-based selectors are fairly slow. If you have a lot of them, it could potentially cause a pretty noticeable slowdown in performance, especially in IE. If you find yourself needing to do this quite a bit, you may want to look at a larger architectural change (if possible).

like image 20
Scott Avatar answered Sep 23 '22 20:09

Scott