jasonayer
jasonayer

Reputation: 41

Having trouble using jQuery change() with checkboxes

I'm trying to add/remove a class and attribute to a few labels and input boxes depending on whether or not a checkbox is checked or not.

By default my check box is set up to be not checked. Here is my existing code...

$("#built").change(function()
{
    $("label.readonly").removeClass("readonly");
    $("input.readonly").removeAttr("readonly");
}).change();

For some reason the event isn't firing. What am I doing wrong? Thanks!

Update:

Here is my html code

<label for="address1" class="readonly">Address Line 1</label>

<input type="text" name="address1" class="readonly" readonly="readonly" value="" />

Also, upon the check box being unchecked I would like the labels and inputs to revert back to its original state of having the readonly class and attribute respectively.

Upvotes: 0

Views: 73

Answers (2)

Albzi
Albzi

Reputation: 15609

Remove the .change() after the function and add a ;.

eg:

$("#built").change(function(){
    $("label.readonly").removeClass("readonly");
    $("input.readonly").removeAttr("readonly");
});

Upvotes: 1

Matt
Matt

Reputation: 343

There is no reason your code won't work, maybe just indentation or something like this.

Here is a jsfiddle with your function and it's working.

I just removed the line break like this:

$("#build").change(function() {
    $("label.readonly").removeClass("readonly");
    $("input.readonly").removeAttr("readonly");
});

Maybe there are other javascript problems somewhere else in the page?

Upvotes: 0

Related Questions