1

I have populated an array with a list of ingredients. I need to make a dynamically create a checkbox & label combo for each object (just the "Name" Object) within the array

here is the code that I am trying to create for each object in the array

<div class="column">
<label for="Doughnuts">Doughnuts</label>
<input type="checkbox" name = "Doughnuts">
</div>

I need to replicate that code fragment for each object in array

Also how would i put the checkbox values into an array to use used later

https://codepen.io/humzaysf/pen/QozzXE

2
  • You forgot to post your own attempted solution, or explain in what way your code is failing. Also to associate a <label> with an <input> the value of the for attribute must be equal the id attribute-value, not the name. Commented Mar 23, 2019 at 0:04
  • I am new to javascript programming. I have been using the following code to get the objects out of the array however I dont know how to build on from that Object.keys(AvailableToppings).forEach(function(key) { console.log(key, AvailableToppings[key]); }); Commented Mar 23, 2019 at 0:12

3 Answers 3

1

var AvailableToppings = [
  {Name: "Pepperoni",ExtraPrice: 0.75},
  {Name: "Tomatoes", ExtraPrice: 0.70},
  {Name: "Anchovies", ExtraPrice: 0.60},
  {Name: "Mushroom", ExtraPrice: 0.50},
  {Name: "Garlic", ExtraPrice: 0.80},
  {Name: "Pinapple", ExtraPrice: 1.00},
  {Name: "Turkey Ham", ExtraPrice: 1.00},
  {Name: "Spicy Beef", ExtraPrice: 1.50},
  {Name: "Spicy Chicken", ExtraPrice: 1.50},
  {Name: "Jalapenos", ExtraPrice: 0.50}
];

var ingreChkboxes = document.getElementById('ingre-chkboxes');

AvailableToppings.map((AvailableTopping, index) => {
  var Name = AvailableTopping.Name;
  var parentDiv = document.createElement('div');
  parentDiv.setAttribute('class', 'column');

  var Label = document.createElement('label');
  Label.setAttribute('for', Name);
  var Node = document.createTextNode(Name + " ");
  Label.appendChild(Node);

  var Input = document.createElement('input');
  Input.setAttribute('type', 'checkbox');
  Input.setAttribute('name', Name);

  parentDiv.appendChild(Label).appendChild(Input);

  ingreChkboxes.appendChild(parentDiv);
});

function submitToppings() {
    var chosenOptions = [];
    var options = document.querySelectorAll("#ingre-chkboxes input[type=checkbox]");

    options.forEach((option) => {
       if(option.checked) {
           chosenOptions.push(option.getAttribute('name'));
       }
    });

    alert(chosenOptions);
}
/* CSS Document */


*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Permanent Marker', cursive;
}

body {
    height: 100vh;

}

h1{
    font-family: 'Alfa Slab One', cursive;
    letter-spacing: 5px
}


	.grid {
    display: flex;
    flex-wrap: wrap;
    text-align: center;
		margin: -1em 0 1em -1em;
  }
  .grid-item {
    flex: 1;
		padding: 1em 0 0 1em;
		margin-top: auto;
		margin-bottom: auto;
  }


/*Ignore above*/



.modal-bb {
	position: relative;
	height: 100%;
}

.modal-bb-contents {
	width: 90%;
	height: 80%;
	overflow: auto;
	margin: auto;
	position: absolute;
	top: 0; left: 0; bottom: 0; right: 0;
	border: solid black;
	text-align: center;
	padding: 3vh 3vh;
}

#button-container {
	display: flex;
	justify-content: right; /* align horizontal */
	align-items: center; /* align vertical */
	border: thin #6A2021 groove;
	padding-left: 2%;
}


#Ingrediants-button {
	background: none;
    border:  none;
    color: rgb(196, 19, 19);
    font-size: 8vh;
    font-family: 'Permanent Marker', cursive;
    cursor: pointer;	
}

.expander-toggle {
	font-size: 10vh;
	padding: 0vh  10vw;
	cursor: pointer;
}

/* Create three equal columns that floats next to each other */
.column {
  float: left;
  width: 33.33%;
  padding: 10vh;
  height: auto; 
	
}


/* Clear floats after the columns */
.row:after {

  display: table;
  clear: both;
}

.ingre-chkbox {
	display: block;
}
<div class="modal-bb">  <!-- Container for modal-box -->
	<div class="modal-bb-contents">	<!-- modal-box -->
		<div id = "button-container">
			<p class="expander-toggle">+</p>
			<button id="Ingrediants-button">Choose Bread</button>				
	  	</div>
		<div class="ingre-chkbox" id="ingre-chkboxes">
		</div>
        <button onClick="submitToppings();">Submit toppings</button>
	</div>
</div>

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

5 Comments

How would i push all checked inputs into an array?
@HumzaYusuf if that data is also available in the object, you can add Input.setAttribute('checked', 'checked'); after the Input.setAttribute('name', Name); . You can wrap it within an statement to check if the input should be pre-checked, like: if(AvailableTopping.isChecked) { Input.setAttribute('checked', 'checked'); }
Hi. I want to store the users checked options into an array which i can reference later. My intention is to show the toppings that a person picked in a bill
You could do this with a submit event for example. I've add some code to the JS and HTML here above to paint the picture
The value of the attribute for of label element must be a single id.
1

AngularJs library is having ng-repeat to display an array. But in pure java you have to update DOM for your array of objects.

1 Comment

not java, but javascript
1

You can do something like this:

const AvailableToppings = [
  {Name: "Pepperoni",ExtraPrice: 0.75},
  {Name: "Tomatoes", ExtraPrice: 0.70},
  {Name: "Anchovies", ExtraPrice: 0.60},
  {Name: "Mushroom", ExtraPrice: 0.50},
  {Name: "Garlic", ExtraPrice: 0.80},
  {Name: "Pinapple", ExtraPrice: 1.00},
  {Name: "Turkey Ham", ExtraPrice: 1.00},
  {Name: "Spicy Beef", ExtraPrice: 1.50},
  {Name: "Spicy Chicken", ExtraPrice: 1.50},
  {Name: "Jalapenos", ExtraPrice: 0.50}
];

let inputs = document.createElement("span");
inputs.innerHTML = AvailableToppings.map(item => `<div class="column">
  <label for="${item.Name}">${item.Name}</label>
  <input type="checkbox" name="${item.Name}" value="${item.ExtraPrice}">
</div>`).join('');
document.getElementById('checkBoxContainer').appendChild(inputs);
/* CSS Document */

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Permanent Marker', cursive;
}

body {
  height: 100vh;
}

h1 {
  font-family: 'Alfa Slab One', cursive;
  letter-spacing: 5px
}

.grid {
  display: flex;
  flex-wrap: wrap;
  text-align: center;
  margin: -1em 0 1em -1em;
}

.grid-item {
  flex: 1;
  padding: 1em 0 0 1em;
  margin-top: auto;
  margin-bottom: auto;
}


/*Ignore above*/

.modal-bb {
  position: relative;
  height: 100%;
}

.modal-bb-contents {
  width: 90%;
  height: 80%;
  overflow: auto;
  margin: auto;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  border: solid black;
  text-align: center;
  padding: 3vh 3vh;
}

#button-container {
  display: flex;
  justify-content: right;
  /* align horizontal */
  align-items: center;
  /* align vertical */
  border: thin #6A2021 groove;
  padding-left: 2%;
}

#Ingrediants-button {
  background: none;
  border: none;
  color: rgb(196, 19, 19);
  font-size: 8vh;
  font-family: 'Permanent Marker', cursive;
  cursor: pointer;
}

.expander-toggle {
  font-size: 10vh;
  padding: 0vh 10vw;
  cursor: pointer;
}


/* Create three equal columns that floats next to each other */

.column {
  float: left;
  width: 33.33%;
  padding: 10vh;
  height: auto;
}


/* Clear floats after the columns */

.row:after {
  display: table;
  clear: both;
}

.ingre-chkbox {
  display: block;
}
<div class="modal-bb">
  <!-- Container for modal-box -->
  <div class="modal-bb-contents">
    <!-- modal-box -->
    <div id="button-container">
      <p class="expander-toggle">+</p>
      <button id="Ingrediants-button">Choose Bread</button>
    </div>
    <!-- Should probably have an ID -->
    <div id="checkBoxContainer" class="ingre-chkbox"></div>
  </div>
</div>

Hope this helps,

1 Comment

The value of the attribute for of label element must be a single id.

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.