Note that there are some explanatory texts on larger screens.

plurals
  1. POadd boxes to a page using a method javascript
    primarykey
    data
    text
    <p>I have an object constructor function here: </p> <pre><code>function Box (id, name, color, number, coordinates) { this.id = id; this.name = name; this.color = color; this.number = number; this.coordinates = coordinates; this.add = function(howMany) { } } </code></pre> <p>I have part of a function here where I get a numeric value (5,10 or 15) that the user chooses with a radio button.</p> <pre><code> function getBoxValues() { var nameInput = document.getElementById("name"); var name = nameInput.value; var numbersArray = dataForm.elements.amount; for (var i = 0; i &lt; numbersArray.length; i++) { if (numbersArray[i].checked) { number = numbersArray[i].value; if (name == null || name == "") { alert("Please enter a name for your box"); return; } else { var newbox = new Box("id", name, color, number, "coordinates"); boxes.push(newbox); addBox(newbox); var data = document.getElementById("dataForm"); data.reset(); } </code></pre> <p>Later, I add boxes to the page with this function:</p> <pre><code> function addBox(newbox) { var scene = document.getElementById("scene"); var div = document.createElement("div"); div.innerHTML += newbox.name; var x = Math.floor(Math.random() * (scene.offsetWidth-101)); var y = Math.floor(Math.random() * (scene.offsetHeight-101)); div.style.left = x + "px"; div.style.top = y + "px"; scene.appendChild(div); newbox.add(number); for (var i = 0; i &lt; howMany; i++) { console.log("added"); addBox(newbox); } } </code></pre> <p>My problem is that I want to add the same number of boxes to the web page as the user chooses with the radio button. So far, my console log tells me "added" for the number of times that the value of "number" is looped through. But, I don't know how to change my code so that new boxes are created for the value of number. I am a beginner at this, so I apologize in advance for any confusion. Would appreciate any help. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload