Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9161567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:59:42+00:00 2026-06-17T13:59:42+00:00

I have written code that creates boxes based on user input in a form.

  • 0

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.

Here is the function where I assign property values to the boxes, and also add them to the page:

function addBox(newbox) {  
   for (var i = 0; i < 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;           
    }                      
  }

Here is the function I have for displaying the info about the boxes:

function display(e) {
   var a = e.target.attributes;
   var b = e.target;
   console.log(b.name);     //shows "undefined"
   alert("id:" + "" + a[0].value); 
  }

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:59:43+00:00Added an answer on June 17, 2026 at 1:59 pm

    You need to use .getAttribute() for an attribute that you set.

    function display(e) {
        var element = e.target || window.event.target;
    
        console.log(element.getAttribute('name'));   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written code that automatically creates CSS sprites based on the IMG tags
I have written code to perform a function that could take a while to
I have written a PHP function to take a video embed code that has
I'have written a simple code, that gets accelerometer&orientation meter and display some graphics based
Hi I have written code for my wix project such that it creates and
I have written a code to dynamically create text boxes from an input of
I have written a function that creates a barplot. I would like to save
I have written a DLL which exports a function that creates a window using
Below, I have written simple code that re-creates problems that have emerged as my
I have written a simple Javascript function (curteousy of codecall.net ) that creates a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.