2

The code below works fine when the checkboxes are within the page itself. When the content in question is loaded from a jquery .load it doesn't register the checkboxes.

The html code below is displays within <div id='output'></div>. It works fine when I move it outside that div.

JS

$(document).ready(function() {
    $('#dropdown').change( function() {
        $('#output').load('/process.php',{dropdown: $(this).val()});
    });

    $(".checkbox").click(function(){
        $('#continue').prop('disabled',$('input.checkbox:checked').length == 0);
    });
});

HTML for process.php

<input type="checkbox" class="checkbox">

3 Answers 3

4

You have to use the on() method to account for elements added to the DOM through AJAX. Try changing this -

$(".checkbox").click(function(){
    $('#continue').prop('disabled',$('input.checkbox:checked').length == 0);
});

To this -

 $('body').on('click', '.checkbox', function(){
     $('#continue').prop('disabled',$('input.checkbox:checked').length == 0);
 });

http://api.jquery.com/on/

Sign up to request clarification or add additional context in comments.

2 Comments

Works perfectly, then I tested it by clicking the cancel button. Which is <button type="reset" class="btn">Cancel</button>. When I click it, the continue button doesn't reset to a disable state (the checkbox is uncheck).
You didn't include any information about a cancel button in your original post so I didn't cover it.
3

Try changing your checkbox handler to

$("body").on("click", ".checkbox", function(){
    $('#continue').prop('disabled',$('input.checkbox:checked').length == 0);
});

Comments

3

maybe you should try it via live binding

$(document).ready(function() {
    ...

    $(document).on( 'click', '.checkbox', function(){
        $('#continue').prop('disabled',$('input.checkbox:checked').length == 0);
    });
});

this way allows you to listen to events also an object that are declared after the event binding was done.

Comments

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.