0

I am trying to make a form with multiple checkboxes. I want to highlight the checkboxes with their content to indicate to the user of the selection
I am using the following layout for my form

HTML

<form>
<div class=labl>
<input type=checkbox id='alpha' />
<label for='alpha'>Checkbox1</label>
</div>

CSS

.labl{

height:50px;
}
.labl:hover{
background:#ccc;
}
.chked {
background: #4285f4;
height:50px;
}

jQuery

<script>

$("input[type=checkbox]").change(function() {
    $(this).parent().toggleClass("chked");
});

</script>


Now when alpha is checked it should change the class of div from labl to chked
but it is not

4
  • 2
    Seems to work to me: jsfiddle.net/EcCr3 Commented Jan 30, 2014 at 12:37
  • jsfiddle.net/EcCr3 is working but it does not work in my code. my code is at techvisionblog.in/ipscan/facebook.html Commented Jan 30, 2014 at 12:44
  • @AshmeetSingh i see no checkbox on that website Commented Jan 30, 2014 at 12:47
  • they are added dynamically after facebook login is completed Commented Jan 30, 2014 at 12:57

4 Answers 4

1

You need a DOM ready handler like this $(function(){...});

$(function () {
    $("input[type=checkbox]").change(function () {
        $(this).parent().toggleClass("chked");
    });
});

Documentation

Update

It appears that the checkbox is dynamically added to the DOM so you must use event delegation like this

    $(document).on("change","input[type=checkbox]",function () {
        $(this).parent().toggleClass("chked");
    });
Sign up to request clarification or add additional context in comments.

6 Comments

@AshmeetSingh what exactly is not working? Is it doing nothing or doing something unexpected? Also check console for any errors.
nothing is working. the page is acting as if there is no jQuery script you can see my page at techvisionblog.in/ipscan/facebook.html
@AshmeetSingh there are no checkboxs in the link
you have to login from via facebook to see the checkboxes but the javascripts are there. checkboxes will appear after facebook login is compelete
@AshmeetSingh are the checkboxes dynamically added to the DOM?
|
1

You can pass both the class names to the toggleClass() method, so that only one of the will be applied at a time

jQuery(function(){
    $("input[type=checkbox]").change(function() {
        $(this).parent().toggleClass("labl chked");
    });
})

Demo: Fiddle

2 Comments

it seems to be working in fiddle but not in my code. In my code it appears just as if there is no jQuery my code is at techvisionblog.in/ipscan/facebook.html
@AshmeetSingh there is no checkbox element in your page
0

just change your script to

$("input[type=checkbox]").change(function() {
    $(this).parents('.labl').toggleClass("chked");
}); 

And your HTML to

<form>
<div class='labl'>
    <input type='checkbox' id='alpha'></input>
<label for='alpha'>Checkbox1</label>
</div>

Here is the updated fiddle

http://jsfiddle.net/k242J/

3 Comments

it seems to work fine over here but in my own script it is not working at all my script is at techvisionblog.in/ipsacn/facebook.html
Where is the checkbox on your site?
they are added dynamically after login from facebook is completed
0

toggleClass functionality is to check the class, if not there it will add, else remove class thats it. its not to change from one class to another.

css

.chked {
    background: #4285f4;
}

js

$("input[type=checkbox]").change(function() {
     $(this).parent().toggleClass("chked");
 });

1 Comment

toggleClass can be used to change one class to another

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.