Note that there are some explanatory texts on larger screens.

plurals
  1. POdisplay properties of an object with event target
    primarykey
    data
    text
    <p>I have written code that creates boxes based on user input in a form. I use a constructor function to give the boxes these values; name, color, id and number. I am supposed to make it so that when the user clicks on the box in the page, an alert box comes up that says what those values are for that individual box. I'm having trouble figuring out how to display most of those properties correctly. </p> <p>Here is the function where I assign property values to the boxes, and also add them to the page:</p> <pre><code>function addBox(newbox) { for (var i = 0; i &lt; newbox.number; i++) { counter++; var scene = document.getElementById("scene"); var div = document.createElement("div"); div.setAttribute("id", counter); div.className += " " + "box"; div.innerHTML += newbox.name; div.style.backgroundColor = newbox.color; 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); div.onclick = display; } } </code></pre> <p>Here is the function I have for displaying the info about the boxes:</p> <pre><code>function display(e) { var a = e.target.attributes; var b = e.target; console.log(b.name); //shows "undefined" alert("id:" + "" + a[0].value); } </code></pre> <p>My alert message works correctly, it shows an individual number for each box, based on the value of the counter. I don't understand why I can't get e.target to work to show name, color or number. I am a beginner, so any thoughts are much appreciated. </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.
    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