1

I would like to display the names of the selected checkboxes when I click Submit.

For example:

  • If no checkbox is checked display should be "Please select a hobby".
  • If painting is checked display "#painting"
  • If painting and reading is checked display "#reading#painting"

The below given code is not working.

function displayHobbies(){
let HobbiesInput=[document.getElementById('dancing'),
document.getElementById('reading'),
document.getElementById('painting')];
var HobbiesError="";
for(var i=0;i<HobbiesInput.length;i++)
{
	if (HobbiesInput[i].checked==false)
	{
            HobbiesError="Please select a hobby";
	}	
	else
	{
	    HobbiesError +="#"+HobbiesInput[i].name;
	}
}
document.getElementById('hobbies_display').innerHTML=HobbiesError;
}
<form name= "myform" onclick="displayHobbies()">

Hobby <input type="checkbox" id="dancing" name="dancing">
  	  <label for="dancing">Dancing</label>
      <input type="checkbox" id="reading" name="reading">
  	  <label for="reading">Reading</label>
  	  <input type="checkbox" id="painting" name="painting">      
  	  <label for="painting">Painting</label>
<button type="button" id="hobby_submit">Submit</button>
</form>
Hobbies:<label id="hobbies_display"></label>

4
  • 2
    Why is there an onclick on the form tag? Don't you want submit? Commented Feb 11, 2019 at 14:50
  • First, you should clear out the hobbies_display label every time something is clicked. Second, your for loop doesn't check to see if they're all unchecked to display that message, it checks if any are unchecked. So if the first two are checked, but the last one isn't, it'll look at that last one, see that it's unchecked, and then set the message to say that nothing is checked. Commented Feb 11, 2019 at 14:53
  • @epascarello its supposed to be onclick...I don't want to submit the form Commented Feb 11, 2019 at 15:02
  • but that is any click inside of the form, not clicking on the button. So submit the form and preventDefault. Commented Feb 11, 2019 at 15:12

4 Answers 4

1

Here is something a little bit more clean:

function displayHobbies() {

  let HobbiesInput = [
      document.getElementById('dancing'),
      document.getElementById('reading'),
      document.getElementById('painting')
  ];

  // flag if no hobby is checked
  var noHobbiesChecked = true;

  // reset display area
  document.getElementById('hobbies_display').innerHTML = "";

  for (var i = 0; i < HobbiesInput.length; i++) {

    if (HobbiesInput[i].checked === true)   {
      // add hobby to display area
      document.getElementById('hobbies_display').innerHTML += "#"+HobbiesInput[i].name;
      // turn off the flag since we have at least one hobby checked
      noHobbiesChecked = false; 
    }

  }

  if (noHobbiesChecked === true) {
    // show error
    document.getElementById('hobbies_display').innerHTML = "Please select a hobby";
  }

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

Comments

0

In your code, if there is an unchecked box, the code will add the "Please select a hobby" message.

Instead, check that at the end, and this will work:

function displayHobbies() {
  let HobbiesInput = [
    document.getElementById('dancing'),
    document.getElementById('reading'),
    document.getElementById('painting')
  ];
  var HobbiesError = "";
  for (var i = 0; i < HobbiesInput.length; i++) {
    if (HobbiesInput[i].checked) {
      HobbiesError += "#" + HobbiesInput[i].name;
    }
  }
  document.getElementById('hobbies_display').innerHTML = HobbiesError || "Please select a hobby";
}
<form name="myform" onclick="displayHobbies()">

  Hobby <input type="checkbox" id="dancing" name="dancing">
  <label for="dancing">Dancing</label>
  <input type="checkbox" id="reading" name="reading">
  <label for="reading">Reading</label>
  <input type="checkbox" id="painting" name="painting">
  <label for="painting">Painting</label>
  <button type="button" id="hobby_submit">Submit</button>
</form>
Hobbies:<label id="hobbies_display"></label>

1 Comment

@MegCullen You can accept an answer (by the checkmark), to help the others find better answers
0

function displayHobbies() {
let HobbiesInput=[document.getElementById('dancing'),
                  document.getElementById('reading'),
                  document.getElementById('painting')];
var HobbiesError="";

for(var i=0;i<HobbiesInput.length;i++)
{
	if (HobbiesInput[i].checked) {
         HobbiesError +="#"+HobbiesInput[i].name;
	}
}

if (!HobbiesError) {
    HobbiesError="Please select a hobby";
}

document.getElementById('hobbies_display').innerHTML = HobbiesError;
}
<form name= "myform" onsubmit="displayHobbies(); return false;">

Hobby <input type="checkbox" id="dancing" name="dancing">
  	  <label for="dancing">Dancing</label>
      <input type="checkbox" id="reading" name="reading">
  	  <label for="reading">Reading</label>
  	  <input type="checkbox" id="painting" name="painting">      
  	  <label for="painting">Painting</label>
<button type="submit" id="hobby_submit">Submit</button>
</form>
Hobbies:<label id="hobbies_display"></label>

Comments

0

Here is a snippet for doing this. We are attaching a listener to a form (not to an every input). Then we look if the thing we clicked is actually an input type checkbox. Then if it checked we push it's value to the array we want to display on top, if not checked we are removing from this array. Then we select a element we want to render our list in, and render. Hope it helps.

let values = [];
const form = document.getElementById('form')
form.addEventListener('click', e => {
  if (!e.target.type === 'checkbox') return;
  const value = e.target.value;
  const checked = e.target.checked;
  if (checked) {
    values.push(value)
  } else {
    const newValues = values.filter(v => v !== value);
    values = newValues;
  }
  const valuesList = document.getElementById('valuesList')
  valuesList.innerHTML = values.map(m => `#${m}`).join('')
})
<form id="form">
  <div id="valuesList"></div>
  <div>
    I have a bike
    <input type="checkbox" name="vehicle1" value="Bike">
  </div>
  <div>
    I have a car
    <input type="checkbox" name="vehicle2" value="Car">
  </div>
</form>

1 Comment

Thank you for the help!! :)

Your Answer

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