4

I'm trying to build a 'radio button' selection style list, but not actually using input type="radio". My backend developer has advised me I need to create this using type="checkbox" in our specific case. I believe this can be done with JS. So how can I make it so that when 1 option is in checked state, the other is unchecked using JS? Here is what I have so far:

http://codepen.io/rjtkoh/pen/VLZrMo

<label for="toggle-1">
<input type="checkbox" id="toggle-1">
<div>option A</div>
  </label>

<label for="toggle-2">
<input type="checkbox" id="toggle-2">
<div>option B</div>
</label>

and CSS:

/* Checkbox Hack */

input[type=checkbox] {
   position: absolute;
   top: -9999px;
   left: -9999px;
}

/* Default State */
div {
   background: green;
   width: 400px;
   height: 100px;
   line-height: 100px;
   color: white;
   text-align: center;
   margin-bottom: 20px;
}

/* Toggled State */
input[type=checkbox]:checked ~ div {
   background: red;
}

I've had a look at other threads talking about changing pseudo classes via JS, but my case dealing with input types confuses me.

0

3 Answers 3

3

Why do you need to do this with a checkbox if the sematics work for radio? There may be a legitimate reason, but without knowing this, I'd suggest you use a radio group instead, which will work without any JavaScript whatsoever, you just need to set a common name attribute on both inputs:

input[type=radio] {
   position: absolute;
   top: -9999px;
   left: -9999px;
}

/* Default State */
div {
   background: green;
   width: 400px;
   height: 100px;
   line-height: 100px;
   color: white;
   text-align: center;
   margin-bottom: 20px;
}

/* Toggled State */
input[type=radio]:checked + div {
   background: red;
}
<label for="toggle-1">
  <input type="radio" name="option" id="toggle-1">
  <div>option A</div>
</label>

<label for="toggle-2">
  <input type="radio" name="option" id="toggle-2">
  <div>option B</div>
</label>

Sign up to request clarification or add additional context in comments.

Comments

1

Here is a pure javascript solution since I don't see jQuery tags in your question:

If you REALLY need to do this with checkboxes you can do this:

html

<div id="radio_group">
<label for="toggle-1">
<input type="checkbox" id="toggle-1">
<div>option A</div>
</label>

<label for="toggle-2">
<input type="checkbox" id="toggle-2">
<div>option B</div>
</label>
</div>

javascript

var options = document.getElementById('radio_group').childNodes;
var checkboxes = document.getElementsByTagName('input');

function uncheck() {
    for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].type == 'checkbox') {
           checkboxes[i].checked = '';
        }
    }
}

function checkBox(e) {
  e.preventDefault();
  if(e.target.nodeName == 'DIV') {
    uncheck();
    e.target.previousElementSibling.checked = 'checked';    
  }
}

for (var i = 0; i < options.length; i++) {
    options[i].addEventListener('click', checkBox, false);
}

heres a fiddle --> https://jsfiddle.net/tL68Lsub/

Comments

0

If you really need the checkbox to work as a radio, you can use:

$("input[type='checkbox']").click(function () {
    if ($(this).is(":checked")) {
        $("input[type='checkbox']").not(this).removeAttr("checked");
        $(this).attr("checked", "true");
    }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.