lundi 3 mai 2021

problem while placing alert on checkbox checked in javascript

i need to check if checkbox 1 or 2 is clicked. error : only background is printing while clicking any checbox and pressing button

function onlyOne(checkbox) {
  var checkboxes = document.getElementsByName('check')
  checkboxes.forEach((item) => {
    if (item !== checkbox) item.checked = false;
  });
}

function run() {

  var checkboxes = document.getElementsByName('check')
  if (document.getElementById('c1').checked) {
    alert("background");
  } else if (document.getElementById('c1').checked) {
    alert("foreground");
  }
}
<input type="checkbox" name="check" id="c1" value="background" onclick="onlyOne(this)">background</input>
<input type="checkbox" name="check" id="c2" value="foreground" onclick="onlyOne(this)">foreground</input>
<input type="button" value="button" onclick="run()">



Aucun commentaire:

Enregistrer un commentaire