Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery checkbox inside div checked one at time

So i have a div thats auto generated and inside are 3 check boxes with different id's & names and by default none of them are checked!! If one happens to get checked then i need the other two unchecked!!

So only one can be checked at a time ...

So all unchecked or only one checked!

<div class="configoptions">
    <table class="configtable" cellpadding="0" cellspacing="0" width="100%">
        <tbody>
            <tr>
                <td class="fieldlabel">Include:</td>
                <td class="fieldarea">
                    <label><input name="configoption[10]" id="co30" value="1" onclick="recalc()" type="checkbox">green</label>
                </td>
            </tr>
            <tr>
                <td class="fieldlabel">Include:</td>
                <td class="fieldarea">
                    <label><input name="configoption[9]" id="co27" value="1" onclick="recalc()" type="checkbox">blue</label>
                </td>
            </tr>
            <tr>
                <td class="fieldlabel">Include:</td>
                <td class="fieldarea">
                    <label><input name="configoption[11]" id="co31" value="1" onclick="recalc()" type="checkbox">white</label>
                </td>
            </tr>
        </tbody>
    </table>
</div>

Here is what i used to uncheck all divs on page ready.. since initially i need them unchecked.. that works great... now i need to make it so only one is check at a time if it is checked! Can someone gimme a hand,? Thx in advance!

$(document).ready(function() {
    $('input[type=checkbox]').each(function() {
        this.checked = false;
    });
});
like image 622
user2159909 Avatar asked Mar 02 '26 03:03

user2159909


1 Answers

Use the prop function to change the checked property of checkbox and radio buttons

$(function(){
    $('input:checkbox').prop('checked', false)
    $('input:checkbox').click(function(){
        if($(this).is(':checked')){
            $('input:checkbox').not(this).prop('checked', false);        
        }
    });
})

Demo: Fiddle

like image 111
Arun P Johny Avatar answered Mar 04 '26 17:03

Arun P Johny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!