Question

I need to set a radio button ('no_subscribe') to true when all the elements, 'checkbox' are unchecked. This should happen after unchecking all these checkboxes. Forgive me if this has been asked elsewhere. I am new to javascript and programming in general, so any help is greatly appreciated! I have been able to make the 'subscribe' button do what I want, but I can't figure out the opposite scenario. These are inside a form called 'mailer'. I recently deleted the attempts I have made. They were not working, so I am sorry there is no code to troubleshoot.

<fieldset>
Subscribe to our Mailing List?
        <input type = "radio" name = "yes" id = "subscribe" onclick = "subscribe_to_all();" value = "yes" />Yes
        <input type = "radio" name = "no" id = "no_subscribe" onclick = "subscribe_to_none();" value = "no" />No
</fieldset>

<fieldset name = "subscribe">
<legend>Mailing List Subscriptions</legend>
    <input type="checkbox" id = "c1" name="subscriptions" onclick = "radio_yes();"  value="CorporateEmails"/>
    Corporate Press Release Emails<br />
    <input type="checkbox" id = "c2" name="subscriptions" onclick = "radio_yes();"  value="SoftwareAdvertising"/>
    Upgrade Software Advertising List<br />
    <input type="checkbox" id = "c3" name="subscriptions" onclick = "radio_yes();"  value="PostBuyoutSpammers"/>
    List given to spammers after buyout<br />
    <input type="checkbox" id = "c4" name="subscriptions" onclick = "radio_yes();"  value="NonCynical"/>
    The only non-cynical list<br />
    <input type="checkbox" id = "c5" name="subscriptions" onclick = "radio_yes();"  value="SelltoSpammers"/>
    List to sell to spammers<br />
    <input type="checkbox" id = "c6" name="subscriptions" onclick = "radio_yes();"  value="SpamList"/>
    Spam List<br />

External javascript file:

function subscribe_to_all() {
    for (i = 0; i < document.mailer.subscriptions.length; i++) {
        document.mailer.subscriptions[i].checked = true;
    }
}

function subscribe_to_none() {
    for (i = 0; i < document.mailer.subscriptions.length; i++) {
        document.mailer.subscriptions[i].checked = false;
    }
}

function radio_yes() {
    document.getElementById("subscribe").checked = true;
}​
Was it helpful?

Solution

I've used a new function. It is not optimized . I've used a new function. There you can fetchthe checkbox status in a loop. But my code works. Just copy and paste and see the changes

    <script type="text/javascript">
function subscribe_to_all() {

for (i=0;i<document.mailer.subscriptions.length;i++){

document.mailer.subscriptions[i].checked = true;

}
}

function subscribe_to_none() {

for (i=0;i<document.mailer.subscriptions.length;i++){

document.mailer.subscriptions[i].checked = false;

}
}
function radio_yes() {

document.getElementById("subscribe").checked = true;

}
function radio_uncheck()
{


        c1=document.getElementById("c1").checked;
        c2=document.getElementById("c2").checked;
        c3=document.getElementById("c3").checked;
        c4=document.getElementById("c4").checked;
        c5=document.getElementById("c5").checked;
        c6=document.getElementById("c6").checked;
        console.log(c1);
        if(c1==false && c2==false && c3==false && c4==false && c5==false && c6==false)
        {document.getElementById("subscribe").checked=false;}

    //document.getElementById("subscribe").checked=false;
} 
</script>

<fieldset>
Subscribe to our Mailing List?
        <input type = "radio" name = "yes" id = "subscribe" onclick = "subscribe_to_all();" value = "yes" />Yes
        <input type = "radio" name = "no" id = "no_subscribe" onclick = "subscribe_to_none();" value = "no" />No
</fieldset>

<fieldset name = "subscribe">
<legend>Mailing List Subscriptions</legend>
    <input type="checkbox" id = "c1" name="subscriptions" onclick = "radio_yes();radio_uncheck();"  value="CorporateEmails"/>
    Corporate Press Release Emails<br />
    <input type="checkbox" id = "c2" name="subscriptions" onclick = "radio_yes();radio_uncheck();"  value="SoftwareAdvertising"/>
    Upgrade Software Advertising List<br />
    <input type="checkbox" id = "c3" name="subscriptions" onclick = "radio_yes();radio_uncheck();"  value="PostBuyoutSpammers"/>
    List given to spammers after buyout<br />
    <input type="checkbox" id = "c4" name="subscriptions" onclick = "radio_yes();radio_uncheck();"  value="NonCynical"/>
    The only non-cynical list<br />
    <input type="checkbox" id = "c5" name="subscriptions" onclick = "radio_yes();radio_uncheck();"  value="SelltoSpammers"/>
    List to sell to spammers<br />
    <input type="checkbox" id = "c6" name="subscriptions" onclick = "radio_yes();radio_uncheck();"  value="SpamList"/>
    Spam List<br />

OTHER TIPS

By using jQuery you can solve this by as follows:

Put class="subscriptions" attribute to all checkboxes.

$('.subscriptions').change(function() {
    var allUncheck = true;
    $('.subscriptions').each(function() {
        if ($(this).is(':checked')) allUncheck = false;
    });
    if (allUncheck == true) {
        $('#no_subscribe').attr('checked', 'true');
    } else {
        $('#no_subscribe').attr('checked', false);
    }
});​

This can be solved using jquery. The radio buttons in the first fieldset should have same name values... I have tried this on your code. Just check it out . I have added a id 'search' for the fieldset...

$(function() {
 $('#search input:checkbox').click(function() {
  var classes = $('#search input:checkbox:checked');  
  if(classes.length == 0)
    {
        $('input:radio[name=no]:nth(1)').attr('checked',true);
    }  
  else
    {
       $('input:radio[name=no]:nth(1)').attr('checked',false);
    }             
});
});

Here is the working fiddle. http://jsfiddle.net/NBCX8/4/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top