Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable a label with jQuery?

How can I disable a label with jQuery? I tried:

$('#some-id').prop('disabled', true);

but it's not grayed out.

My HTML is: <label for="some-id">Label Here</label><input id="some-id"/>

like image 487
tuco Avatar asked Jul 09 '13 15:07

tuco


People also ask

Can you disable a label?

A label can't be disabled. One of the effects it has is to extend the click target of a form control, so you probably want to disable the form control instead. However, for some reason, all your labels are associated with the same control (the one with id="u" ), which suggests that you aren't using <label> correctly.

How do you disable elements in jQuery using ATTR?

We can easily disable input box(textbox,textarea) using disable attribute to “disabled”. $('elementname'). attr('disabled','disabled'); To enable disabled element we need to remove “disabled” attribute from this element.

How do I turn off click on labels?

pointer-events:none; display:block; Display block is required if the label contains other block level elements.

How do I disable an element?

An element can be disabled in HTML by setting disable property to true and enabled again by setting disabled=false. By using jQuery, you can grab the element you want to enable or disable and change this property by using the prop() or attr() function, depending upon the version of jQuery you are using.


2 Answers

labels do not have a disable property inherently built in. You will need to add a class to disable them yourself.

Something like:

.disabled {
   color: darkgrey;
   background-color: grey;
}

And to add the class to your element:

$('#some-id').addClass('disabled');
like image 124
Igor Avatar answered Sep 22 '22 08:09

Igor


To disable a label, disable the form control it is associated with.

That won't change the appearance of the label though, for that you have to change the CSS that applies to it. That is best done by adding (or removing) a class from the element (and having a pre-existing CSS ruleset that matches that class).

like image 37
Quentin Avatar answered Sep 18 '22 08:09

Quentin