// Create an array to hold all of the square objects
var squares = [];
// Create the Square "class"
function Square(size, color) {
this.size = size;
this.color = color;
}
function writeToPage (aSquare) {
var myCanvas = document.getElementById("squareCanvas");
console.log(aSquare);
// Create new paragraph node
var paragraph = document.createElement("p");
// Set the style of the paragraph
paragraph.style.height = aSquare.size + "px";
paragraph.style.width = aSquare.size + "px";
paragraph.style.backgroundColor = aSquare.color;
// Add paragraph to the "Canvas"
myCanvas.appendChild(paragraph);
}
function submitSquare() {
// Get values
var size = document.getElementById("squareSizeId").value;
var color = document.getElementById("squareColorId").value;
console.log("Size: " + size + "; Color: " + color);
// Write the square object to the page
var mySquare = new Square(size, color);
writeToPage(mySquare);
// Add square object to an array of square objects
squares.push(mySquare);
console.log(squares);
// Write the array to local storage
var jsonString = JSON.stringify(squares);
console.log(jsonString);
localStorage.setItem("squareStorage", jsonString);
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.