views:

203

answers:

2

I am trying to allow a click function if the user has checked a checkbox on the page. Otherwise, I want to add a class to the #additional_foreign button.

I think I am close, but it doesn't seem to be working. Here is my code:

if($('#foreign_checkbox').attr('checked')) { 
    $('#additional_foreign').click(function() {
        alert('This click function works');
    });
} else {
    $('#additional_foreign').addClass('btn_disable');   
}

When I check the checkbox, it doesn't allow the click function and when I uncheck the checkbox, it also doesn't add the class as it should.

What am I doing wrong?

EDIT:Here is my HTML for clarification.

<input id="foreign_checkbox" type="checkbox" />
<span id="additional_foreign">Click Me</span>
+3  A: 

try using $('#foreign_checkbox').is(":checked") - rest of the code looks fine

If this was my code I'd do something like this to make it work:

<input id="foreign_checkbox" type="checkbox" />
<span style='display:none' id="additional_foreign">Click Me</span>

<script type="text/javascript">
    $(document).ready(function() {

        $("#foreign_checkbox").click(function() {
            if($('#foreign_checkbox').is(':checked')) { 
                $('#additional_foreign').show();
            } else {
                $('#additional_foreign').hide();
            }
        });

        $('#additional_foreign').click(function() {
            alert('This click function works');
        });
    });
</script>
Marek Karbarz
When I do that, the else part of the conditional statement works, but the click function still does not work when I check the checkbox
zeckdude
You also added your inner doSomething() function to the onclick handler of #additional_foreign, which I doubt you meant to do.
Brock Batsell
you mean nothing happens when you check the `additional_foreign` checkbox? First I would move the `click` event binding outside of the if statement - you don't wan to bind it every time `foreign_checkbox` is checked - you'll end up with multiple events firing. Then inside the if statement I would simply hide/show the other checkbox. Would you care to post some of your HTML?
Marek Karbarz
Brock - No I want to make the #additional_foreign button clickable after the checkbox is clicked. If the user then clicks on the button, it should run the doSomething() function.
zeckdude
Works great! I think I was overthinking it too much. Your solutions seems much easier and more user-friendly. Thanks!
zeckdude
Please don't use any special jQuery methods to get or set the checkedness of a checkbox. DOM has a perfectly good way of doing this that couldn't possibly be any simpler or clearer, and has been supported in all browsers since the late 1990s, which is that checkbox elements have a boolean `checked` property. Using jQuery's `is` or `attr` will slow things down, make the code less clear and add potential for error.
Tim Down
@Tim - personal preference, nothing wrong with it
Marek Karbarz
+1  A: 

The problem is that the code doesn't run continually, only when the page loads, when all the boxes are unchecked. You need an action that fires when the box is checked or unchecked, which adds an action or a class to the button:

$('#foreign_checkbox').change(function(){
    if (this.checked){
        $('#additional_foreign').click(function() {
           doSomething();
        });
    } else {
        $('#additional_foreign').addClass('btn_disable');
    }   
});
Isaac Cambron