Rene
Rene

Reputation: 642

only select one checkbox

I would like to build a javascript so a user can choose only one option between the the mentioned below. Could you give me some pointers how to do this since I am a javascript noob.

Thank you!

This is the picture of the part of a menu enter image description here

<td><label for="dock_books_search_visible_online"> Visible online?</label></td>
                        <td><input type="checkbox" name="option" value="checkedVisibleOk" id="dock_books_visible_ok"  /> </td>
                        <td><label for="dock_books_search_visible_online_yes"> Yes</label></td>
                        <td><input type="checkbox" name="option" value="checkedVisibleNok" id="dock_books_visible_nok" /> </td>
                        <td><label for="dock_books_search_visible_online_no"> No</label></td>

Upvotes: 3

Views: 24783

Answers (7)

50dollanote
50dollanote

Reputation: 189

Use radio buttons and ensure that the name tag is consistent with all options and it'll automatically select just one w/o the need for additional code or JS.

Upvotes: 0

Parikshit Sharma
Parikshit Sharma

Reputation: 1

function toggle(chkBox, field) {
    for ( var i = 0; i < field.length; i++) {
        field[i].checked = false;
    }
    chkBox.checked = true;
}


<td>
    <INPUT type="checkbox" name="xyz" onClick="toggle(this,document.myform.xyz);" value="${incident.incidentID}">
</td>

Upvotes: 0

talha2k
talha2k

Reputation: 25650

For single selection from multiple options we use Radio Buttons not CheckBoxes.

You should use some thing like this.

<input type="radio" name="option" value="Yes" id="yes" /> 
<input type="radio" name="option" value="No" id="no" /> 

But still if you want to go the other way round, Just add the following script in your head tag:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(':checkbox').bind('change', function() {
        var thisClass = $(this).attr('class');
        if ($(this).attr('checked')) {
            $(':checkbox.' + thisClass + ":not(#" + this.id + ")").removeAttr('checked');
        }
        else {
            $(this).attr('checked', 'checked');
        }
    });
});
</script>

Here is the fiddle for above.

Hope this helps.

Upvotes: 9

shareef
shareef

Reputation: 9581

you got a point to use radio buttons any way here is the javascript solution i used it in my project when there is search criteria and search result in data grid by ajax having 13 records when i check one record it disables the rest

code for javascript enable disable check boxes jsfiddle

<form  name="mainForm" method="GET">

   Visible online?


       <input type="checkbox" name="option" value="checkedVisibleOk" id="option"  onclick="changeCheckBox();"/>

 yes

        <input type="checkbox" name="option" value="checkedVisibleNok"     id="option" onclick="changeCheckBox();"/>

       no

</form>
<script>
var serNoChecked="";
function changeCheckBox() {
 try {

        var max = document.mainForm.option.length;
        var count = 0;

        for (var i = 0; i < max; i++) {
            if (document.mainForm.option[i].checked == true) {
                count++;
                serNoChecked = i;
            }
        }
        if (count == 1) {
            for (var i = 0; i < max; i++) {
                if (document.mainForm.option[i].checked == false) {
                    document.mainForm.option[i].disabled = true;
                }
            }
        }
        else if (count == 0) {
            for (var i = 0; i < max; i++) {
                document.mainForm.option[i].disabled = false;
            }
        }

        if (null == max) return false;
        if (count == 0) {
            return true;
        }
        else if (count > 0) {
            return false;
        }

    }
    catch (e) {
        alert(e.message);
    }
}
    </script>

Upvotes: 2

Anil
Anil

Reputation: 21910

Try using Radio Button's, Give them the same name to group them and only allow 1 to be selected:

<td>
    <label for="dock_books_search_visible_online"> Visible online?</label>
</td>
<td>
    <input type="radio" name="option" value="checkedVisibleOk" id="dock_books_visible_ok"  /> 
</td>
<td>
    <label for="dock_books_search_visible_online_yes"> Yes</label>
</td>
<td><input type="radio" name="option" value="checkedVisibleNok" id="dock_books_visible_nok" />
</td>
<td>
    <label for="dock_books_search_visible_online_no"> No</label>
</td>

Check this JSFiddle.

Upvotes: 1

Awais Qarni
Awais Qarni

Reputation: 18006

Hi why are you using checkbox? Checkboxes are not for the functionality that you want. Radio buttons are exact what you want to use.

 <form>
 <input type="radio" name="sex" value="male" /> Male<br />
 <input type="radio" name="sex" value="female" /> Female
 </form> 

For further details look here

Upvotes: 0

graphicdivine
graphicdivine

Reputation: 11211

This looks like a job for radio buttons, not checkboxes.

Upvotes: 2

Related Questions