Checked boxes

I think this can be done in Javascript but not sure how to do it. Any help would be appreciated:

I have 2 radio buttons - Seller and Buyer

What I want is that if Seller radio button is clicked then what I have labeled OPTION #1 would be checked and if Buyer radio button is clicked the OPTION #2 as labeled below would be checked.

 <tr>
<td width="822" align="right" height="1" colspan="6">
<p align="center"><center><input type="radio" value="Seller" name="SellerorBuyer">For
Seller&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="radio" name="SellerorBuyer" value="Buyer">&nbsp;
For Buyer</center> </td>
</tr>

THIS ONE IF SELLER: OPTION #1

<td width="1" align="left" height="22" bgcolor="#c4d7dd"><input type="checkbox" name="Owners" value="ON" tabindex="12"></td>
<td width="250" height="22" bgcolor="#c4d7dd"><font face="Verdana" size="1">Owner's
Title Policy (ALTA Homeowners)</font></td>

THIS ONE FOR BUYER: OPTIONS #2


<td width="1" height="21" bgcolor="#c4d7dd"><input type="checkbox" name="Combo" value="ON" tabindex="15"> </td>
<td width="309" height="21" bgcolor="#c4d7dd"><font face="Verdana" size="1">Lender's Extended Policy (Combo rate)</font> </td>

Thank you!

First, you must have the server-side code check that the appropriate things are selected, and give a suitable error response if they’re not. That’s for your own protection.

When the option is changed, the best way is to uncheck all relevant elements, and then check only one of them.

function personOptionEventHandler(evt) {
  var form = evt.target.form;
  form.elements.Owners.checked = false;
  form.elements.Combo.checked = false;
  if (evt.target.value === "Seller") {
  	form.elements.Owners.checked = true;
  }
  if (evt.target.value === "Buyer") {
  	form.elements.Combo.checked = true;
  }
}

var form = document.querySelector("#myForm");
form.elements.SellerorBuyer.forEach(function (option) {
  option.addEventListener("change", personOptionEventHandler);
});

But what happens if someone tries to check the other checkbox?
But what happens when someone unticks the checkbox?
But what happens when someone checks one of the checkboxes?

  • When a checkbox is ticked, disable the other checkbox.
  • When no checkboxes are ticked, unselect the radio buttons.
  • When someone ticks a checkbox, select the appropriate radio button.

So, with those common usage situations in place, you have the following code:

function selectRadio(radioGroup, index) {
	radioGroup.forEach(function (radio) {
  	radio.checked = false;
  });
  radioGroup[index].checked = true;
}
function deselectAllRadio(radioGroup) {
	radioGroup.forEach(function (radio) {
  	radio.checked = false;
  });
}
function resetCheckboxes(checkboxes) {
  checkboxes.forEach(function (checkbox) {
    checkbox.checked = false;
  	checkbox.disabled = false;
  });
}
function personRadioEventHandler(evt) {
  var form = evt.target.form;
  resetCheckboxes([
  	form.elements.Owners,
    form.elements.Combo
  ]);
  if (evt.target.value === "Seller") {
  	form.elements.Owners.checked = true;
  	form.elements.Combo.disabled = true;
  }
  if (evt.target.value === "Buyer") {
  	form.elements.Combo.checked = true;
  	form.elements.Owners.disabled = true;
  }
}
function ownerCheckboxEventHandler(evt) {
	var form = evt.target.form;
  if (evt.target.checked) {
    selectRadio(form.elements.SellerorBuyer, 0);
  	form.elements.Combo.checked = false;
    form.elements.Combo.disabled = true;
  } else {
	  deselectAllRadio(form.elements.SellerorBuyer);
    form.elements.Combo.disabled = false;
  }
}
function comboCheckboxEventHandler(evt) {
	var form = evt.target.form;
  if (evt.target.checked) {
    selectRadio(form.elements.SellerorBuyer, 1);
  	form.elements.Owners.checked = false;
  	form.elements.Owners.disabled = true;
  } else {
	  deselectAllRadio(form.elements.SellerorBuyer);
    form.elements.Owners.disabled = false;
  }
}

var form = document.querySelector("#myForm");
form.elements.SellerorBuyer.forEach(function (option) {
  option.addEventListener("change", personRadioEventHandler);
});
form.elements.Owners.addEventListener("change", ownerCheckboxEventHandler);
form.elements.Combo.addEventListener("change", comboCheckboxEventHandler);

Which can be experimented with at https://jsfiddle.net/hx8hd8ft/2/

Thanks for your reply, I appreciate that. Now I know why I couldn’t figure this out with an internet search!

The jsfiddle link sample code doesn’t seem to work, but I did but all of your code under the tag and nothing happens.

Did I not completely understand or do that correctly?

Thanks!

If the sample doesn’t work then I need to fix that up. What didn’t work about the jsfiddle sample?

It didn’t make the selection when clicking on Seller or Buyer.

Is sounds like you might be using an older type of web browser. What version of web browser are you using?

In the meantime, I have replaced the forEach methods with simpler loops, in case you are using something like internet explorer.

It does now work in IE. The code I pasted didn’t work in any browser.

I didn’t see it, but do I need to add an id or name in the or input tag to determine what needs to be checked?

This is the code that I pasted in the head, just before the form tag:

<script>

    function personOptionEventHandler(evt) {
        var form = evt.target.form;
        form.elements.Owners.checked = false;
        form.elements.Combo.checked = false;
        if (evt.target.value === "Seller") {
            form.elements.Owners.checked = true;
        }
        if (evt.target.value === "Buyer") {
            form.elements.Combo.checked = true;
        }
    }

    var form = document.querySelector("#myForm");
    form.elements.SellerorBuyer.forEach(function (option) {
        option.addEventListener("change", personOptionEventHandler);
    });
    
    </script>

<script>
    function selectRadio(radioGroup, selectedIndex) {
        for (var i = 0; i < radioGroup.length; i += 1) {
            radioGroup[i].checked = false;
        }
        radioGroup[selectedIndex].checked = true;
    }
    function deselectAllRadio(radioGroup) {
        for (var i = 0; i < radioGroup.length; i += 1) {
            radioGroup[i].checked = false;
        }
    }
    function resetCheckboxes(checkboxes) {
        for (var i = 0; i < checkboxes.length; i += 1) {
            checkboxes[i].checked = false;
            checkboxes[i].disabled = false;
        }
    }
    function personRadioEventHandler(evt) {
        var form = evt.target.form;
        resetCheckboxes([
  	form.elements.Owners,
    form.elements.Combo
  ]);
        if (evt.target.value === "Seller") {
            form.elements.Owners.checked = true;
            form.elements.Combo.disabled = true;
        }
        if (evt.target.value === "Buyer") {
            form.elements.Combo.checked = true;
            form.elements.Owners.disabled = true;
        }
    }
    function ownerCheckboxEventHandler(evt) {
        var form = evt.target.form;
        if (evt.target.checked) {
            selectRadio(form.elements.SellerorBuyer, 0);
            form.elements.Combo.checked = false;
            form.elements.Combo.disabled = true;
        } else {
            deselectAllRadio(form.elements.SellerorBuyer);
            form.elements.Combo.disabled = false;
        }
    }
    function comboCheckboxEventHandler(evt) {
        var form = evt.target.form;
        if (evt.target.checked) {
            selectRadio(form.elements.SellerorBuyer, 1);
            form.elements.Owners.checked = false;
            form.elements.Owners.disabled = true;
        } else {
            deselectAllRadio(form.elements.SellerorBuyer);
            form.elements.Owners.disabled = false;
        }
    }

    var form = document.querySelector("#myForm");
    form.elements.SellerorBuyer[0].addEventListener("change", personRadioEventHandler);
    form.elements.SellerorBuyer[1].addEventListener("change", personRadioEventHandler);
    form.elements.Owners.addEventListener("change", ownerCheckboxEventHandler);
    form.elements.Combo.addEventListener("change", comboCheckboxEventHandler);
    
    </script>

There’s your problem. The head is a bad place for scripting code because the HTML elements do not yet exist until the body is processed.

Move the script down to where it’s supposed to be at the end of the body, just before the </body> tag.

Well there you go! I had that problem last time - you would think I should know that; now I do!!

So it works great, and I see that when making the selection, it grays out one of the choices. If there a way that if the Seller wanted to pay for both, or vise versa, it leaves the check box available to select?

Yes, just remove all of the lines that say disabled, and the checked=false lines in the checkbox event handlers too.

Trying your online code…

If I click Seller, it checks the first option.

Then when I click the second option, both options are check, but if jumps to the Buyer as selected.

So I want it available where either party can select both, but have the default to the original code.

We’ll need some extra code then so that the the radio buttons are not changed when both checkboxes are checked, and also so that when both are checked and one is onchecked, the radio will switch over to the remaining checkbox.

Here’s that update. https://jsfiddle.net/hx8hd8ft/6/

Brilliant!

You are very good at scripting! I appreciate your help and thank you for your time.

Thanks!

PS. It’s not obvious to me but is there a way to rate your great work, like a point system like some forums have? If so, let me know where that would be.

One last question that I just noticed:

If I click on Buyer and the 2nd option is checked - that’s great.

But if I uncheck the 2nd option, the Buyer radio unselected.

I want it where if the options are unchecked, the radio buttons of the Seller and Buyer are still checked.

Thanks

It seems like you are using the wrong form elements for the type of interaction that you intend.
However, remove the deselectAllRadio function, and remove all lines that placed a call to it.

I removed the function and the 2 lines underneath that. However, if I check the 2nd option, without selecting the radio buttons, it then selects the Seller. Same with selecting the 1st option, selects the Seller radio button.

Then it’s time to start this all over again, because there are several different and confusing requirements.

With no JavaScript code at all, what are all of the things that the scripting must achieve. Please be as detailed as you can.

Have the default as before with the radio buttons. After that, if a user doesn’t select Seller or Buyer and then (by accident) checks on one of the options, then neither Seller or Buyer would be check and it would be up to the user to select one of those options.

Okay, so the radio buttons are not affected in any way at all by the checkboxes,