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?
Use jQuery . hide()[^] function. $('#lblId'). hide(); // Replace the correct ID of Label.
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.
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.
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.
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();
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With