tags:

views:

27

answers:

3

I have 2 checkbox rules that are overlapping. This one runs first

$("#BMFNP").change(function() {
    if($(this).is(":checked") && $("#INTEF").is(":not(:checked)")) {
        $("#INTEF").attr("checked", true);
        alert("foo");
    } 
});

This one runs second:

$("#BMFNP").change(function() {
    if($(this).is(":checked") && $("#INTEF").is(":checked")) {
    alert("bar");
    }
});

So basically the first one runs if BMFNP is checked, and INTEF isn't it checks INTEF and runs the alert. At that time, both are checked so it runs the second function. How can I fix this? I need both to work and display different messages for each situation, if BMFNP is checked, INTEF isn't check INTEF and alert it has been added and BMFNP can only do xxxxx. If both are checked, simply alert that BMFNP can only do xxxx, no need to alert it has been added.

Thanks,

+2  A: 
$("#BMFNP").change(function() {
    if($(this).is(":checked") {
       if( $("#INTEF").is(":checked")) {               
            alert("bar");
       } else {
            $("#INTEF").attr("checked", true);
            alert("foo");
       }
    } 
});

like that?

Reigel
Close, but it was running alert bar if both were checked and you removed BMFNP. You got me to re evaluate it and came up with this and it worked as desired. $("#BMFNP").change(function() { if($(this).is(":checked")) { if($("#INTEF").is(":checked")) { alert("bar"); } else { $("#INTEF").attr("checked", true); alert("foo"); } } });thanks man!
Dirty Bird Design
sorry, my bad, got it fixed.. :)
Reigel
+2  A: 

You can use a single event handler, and significantly shorten the solution:

$("#BMFNP").change(function() {
    if(this.checked) {
        $("#INTEF").is(":checked") ? alert('bar') : alert('foo');
    }
});
karim79
This is easier, and it does exactly what you asked for.
drachenstern
`alert` is just a simple sample.... I doubt if you can put a lot of codes there?... like do xxxxx??? and you missed 1 `(`
Reigel
@Reigel, I should have been more explicit, that was meant to be text as in "This can only do xxxx(something)" it is a product that can only do something, I didn't mean a command in the JS. my bad!
Dirty Bird Design
A: 

The comments are difficult to format code in. This does exactly what has been asked for.

$("#BMFNP").change(function() {
  if($(this).is(":checked")) {
        if($("#INTEF").is(":checked")) {
              alert("only outside brazil");
        } else { $("#INTEF").attr("checked", true);
            alert("INTEF has been added and only avail. outside brazil");
            }
    }
});

@karim79 - yours is shorter, but only runs the alerts, I need to add checked to INTEF if it isn't and BMFNP is like above. Thanks for all your help guys!

Dirty Bird Design
hahaha my bad... I forgot to transfer the `$("#INTEF").attr("checked", true);` part... it was supposed to go with `alert('foo')`... :D... sorry for that... well, I'm glad you figured it out.. :)
Reigel