Jump to content

Checkbox Javascript Toggle


Recommended Posts

I have a submission form with a couple of checkboxes.  Ideally, it would be mandatory for one of the two checkboxes to be selected.  However, my primary concern is that both checkboxes cannot be selected so I want to toggle off one when the other is checked.  I have been playing around with other javascript code I have found on the Caspio forum but have not had any success getting it modified and working.  Following is my attempted code on two checkboxes, one named "pure" and the other "mix".  Any help for a javascript newcomer would be appreciated...

 

<script type="text/javascript">
/* Get value of the Yes/No fields */
var cb_pure = '[@field:pure]';

var cb_mix = '[@field:mix]';
if (cb_pure == 'Yes')
{
/* make sure cb_mix is deselected */
    document.getElementById("InsertRecordmix").checked=false;
}
if (cb_mix == 'Yes')
{
/* make sure cb_pure is deselected */
    document.getElementById("InsertRecordpure").checked=false;
}
</script>

Link to comment
Share on other sites

Hello sddenrweb,

 

I think, you can try the following code:

<script type="text/javascript">
function turnOffMix()
{
if (document.getElementById("InsertRecordmix").checked)
{ document.getElementById("InsertRecordmix").checked = false; }
}

function turnOffPure()
{
if (document.getElementById("InsertRecordpure").checked)
{ document.getElementById("InsertRecordpure").checked = false; }
}

document.getElementById("InsertRecordpure").onchange=turnOffMix;
document.getElementById("InsertRecordmix").onchange=turnOffPure;
</script>
Link to comment
Share on other sites

 

Hello sddenrweb,

 

I think, you can try the following code:

<script type="text/javascript">
function turnOffMix()
{
if (document.getElementById("InsertRecordmix").checked)
{ document.getElementById("InsertRecordmix").checked = false; }
}

function turnOffPure()
{
if (document.getElementById("InsertRecordpure").checked)
{ document.getElementById("InsertRecordpure").checked = false; }
}

document.getElementById("InsertRecordpure").onchange=turnOffMix;
document.getElementById("InsertRecordmix").onchange=turnOffPure;
</script>

 

Thank you Caspio Guru!  I tried it and It works perfectly fine -- should have realized some sort of triggering event would be needed.

Link to comment
Share on other sites

  • 8 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...