/    Sign up×
Community /Pin to ProfileBookmark

All my boxes have the same id of "box4"

//Invoke functions only after page has fully loaded
//The box id needs to start at zero
//increment to what radio button clicked
window.onload = init;

var boxes = [];

var counter = 0;

function Box(id, name, color, x, y) {
this.id = id;
this.name = name;
this.color = color;
this.x = x;
this.y = y;
}

function init() {
var generateButton = document.getElementById(“generateButton”);
generateButton.onclick = generate;

var clearButton = document.getElementById(“clearButton”);
clearButton.onclick = clear;

}

function generate() {
var data = document.forms.data;
var textInput = document.getElementById(“name”);
var name = textInput.value;
if (name == null || name == “”) {
alert(“Please give your Amazing Box a name”);
return;
}

var colorSelect = document.getElementById(“color”);
var colorOption = colorSelect.options[colorSelect.selectedIndex];
var color = colorOption.value;
if (!color) {
alert(“Pick a color”);
return;
}

// Get count of boxes to create
var boxCount = 0;
var radioButtons = document.getElementsByName(“amount”);
for(iRadio = 0; iRadio < radioButtons.length; iRadio++)
{
if(radioButtons[iRadio].checked)
{
boxCount = radioButtons[iRadio].value;
}
}

if(boxCount == null || boxCount == 0)
{
alert(“Select an amount of boxes to create”);
return;
}

// Create boxes
for (i = 0; i < boxCount; i++) {
var scene = document.getElementById(“scene”);
var x = Math.floor(Math.random() * (scene.offsetWidth-101));
var y = Math.floor(Math.random() * (scene.offsetHeight-101));

boxes.push(new Object());

var div = document.createElement(“div”);
div.id = “box” + i;
div.style.left = x + “px”;
div.style.top = y + “px”;
div.style.backgroundColor = color;
div.setAttribute(“class”, “box”);

scene.appendChild(div);
div.innerHTML = “Box of ” + name + “<br />(click me)”;

div.onclick = function() {
alert(“You clicked on a box with id ” + div.id +
“, named Box of ” + name + “, whose color is ” + color +
” at position ” + div.style.top + “, ” + div.style.left);

}

data = document.getElementById(“data”);
data.reset();
}
}

//Clear the boxes from scene div

function clear() {
var sceneDivs = document.querySelectorAll(“div#scene div”);
for (var i = 0; i < sceneDivs.length; i++) {
var scene = document.getElementById(“scene”);
var cutList = document.getElementsByTagName(“div”)[1];
scene.removeChild(cutList);
}
}

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JLeonauthorFeb 17.2015 — The idea is that the id property given to each div will mirror that of an index in the array for that box. So the div with the id 0 is a representation of the Box() object with the index 0 in the boxes array. That way when display is clicked, how do I get the id of the div which was clicked, grab that Box() object at that index of the array, and show its properties in an alert.
×

Success!

Help @JLeon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...