I have two check boxes, 'a1' and 'p1'. I simply need a script that will either have nothing checked or only 'a1' or only 'p1' checked.
I was using(vice versa for the other checkbox):
and this worked fine, on the PC, however when using the IPad, this script would not function.
are 'a1' and 'p1' names or IDs?
sorry, 'a1' is the name i have assigned the checkbox
assuming that they are both names, and that they are the first elements on the page with those names, and that in the case of both being checked you only want a1 to remain checked...
<input type="checkbox" name ="a1"/>
<input type="checkbox" name ="p1"/>
<input type="button" onclick ="checkIt()" value = "check boxes"/>
var cb1 =document.querySelector('input[name="a1"]');
var cb2 =document.querySelector('input[name="p1"]');
I don't understand why you are trying to make checkboxes act like radio buttons. The quoted description of the behavior you are trying to script is exactly why radio buttons exist. Example:
Originally Posted by coolhandbr
No scripting required and it works cross-browser and cross-device. You can, however, script the buttons to do other things if you have that need.
<input type="radio" name="choices" id="a1" value="choice 1"> <label for="a1">Choice 1</label><br>
<input type="radio" name="choices" id="p1" value="choice 2"> <label for="p1">Choice 2</label><br>
<input type="radio" name="choices" id="clearChoices" value=""> <label for="clearChoices">None</label> <!-- Optional. Submits nothing. Exists to let the user change his/her mind :) -->
Last edited by Kevin2; 07-02-2014 at 05:56 PM.
Reason: oops fix
I'm with Kevin2 -- first thing I was thinking was "why not just use radio buttons" since they already do what you're trying to waste JS doing.
With Kevin and deathshadow on this, radio is the way to go.
Only thing I can think of is if you are going to have multiple check boxes with various combos to select and not all options will be available depending on what is checked... Even then you can use multiple radio sets and have those selections of sub sets determined on the main set choice.
Please remember to wrap any code you have in forum tags:-
[CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]
If you can't think outside the box, you will be trapped forever with no escape...
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)