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"/>
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"/>
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');
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).
You can disable an input like this:
$("#test").attr("disabled", "disabled");
It should be noted that you can't disable a label, as it doesn't accept any input to begin with. If you want it to look disabled, you might consider just changing the color of the text to gray.
$("#test").css("color", "#666");
You should add a disabled class to the label when you disable the input. You could do it like this if you want to keep all the code in the same place:
$('#some-id, [for="some-id"]').prop('disabled', true).addClass('disabled');
Working JSFiddle example.