Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I store html properties in an object?
    text
    copied!<p>I am experementing with javascript objects for the first time and need some help. I want to store generated user input in objects, push them into an array and later on reuse them. So far I have come to this: </p> <pre><code> function changeColors() { //get the numbers from the html var rd = parseInt(document.getElementById("red").value); var gr = parseInt(document.getElementById("green").value); var bl = parseInt(document.getElementById("blue").value); var op = parseFloat(document.getElementById("opacity").value); //convert the decimal into hexadecimal var rdhex = (rd &lt; 16) ? "0" + rd.toString(16) : rd.toString(16); var grhex = (gr &lt; 16) ? "0" + gr.toString(16) : gr.toString(16); var blhex = (bl &lt; 16) ? "0" + bl.toString(16) : bl.toString(16); //concatenate all hex to generate a color var hexcode = "#" + rdhex + grhex + blhex; //view the change in the browser document.getElementById("div").style.backgroundColor = hexcode; document.getElementById("colordisplay").innerHTML = hexcode; //change opacity document.getElementById("div").style.opacity = op; </code></pre> <p>Here I get all the input that I need to store and in the next function I am trying to store it in an object and array:</p> <pre><code> function Save(){ var colors = {}; var nextColor = [] colors.nextColor = nextColor; console.log(colors); var rgb = document.getElementById("colordisplay").innerHTML; var opacity = document.getElementById("div").style.opacity; var name = document.getElementById("name").value; var nextColor = { "name": name, "rgb": rgb, "opacity": opacity } colors.nextColor.push(nextColor); console.log(colors); } </code></pre> <p>My question is: is how wrong is that and how it can be corrected? Thank you!</p>
 

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