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 6148291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:15:19+00:00 2026-05-23T19:15:19+00:00

I’m creating a product customizer with HTML, CSS and Javascript, and I’ve set up

  • 0

I’m creating a product customizer with HTML, CSS and Javascript, and I’ve set up a custom object called “Component.” I’m creating a new instance of Component with the “new” keyword for each individual component of each product, as they will be manipulated independently based on user input. Each component has a div in the HTML that looks like this:

<div class="component" id="ABC123">
                <div class="checkBox"><div class="checkmark"> </div>  </div>
                <div class="nameLabel"><h4>ABC-123</h4> </div>
                <div class="priceLabel"><h5 id="ABC123-PRICE"></h5> </div>
            </div> <!-- ABC 123 -->

And here is the relevant javascript:

function Component(id, model, htmlid, cost) {
this.id = id;
this.model = model;
this.htmlid = htmlid;
this.cost = cost;
this.element = document.getElementById(htmlid);
this.picture = document.getElementById(htmlid + "-PIC");
this.plabel = document.getElementById(htmlid + "-PRICE");
this.active = false;

//****This is the problem line:****
//this.element.onclick = function() {
//                             selectUnselect(this);
//                          };
//this.element.onclick = selectUnselect(this);
}


// Set: VAR NAME, MODEL, HTML ID, COST
var ABC123 = new Component(ABC123, ABCSeries, "ABC123", 100);
var DEF456 = new Component(DEF456, ABCSeries, "DEF456", 200);
var GHI789 = new Component(GHI789, ABCSeries, "GHI789", 300);


function selectUnselect(component) {
console.log(component);
//Simplified for troubleshooting
}

As far as the components go, I’m going to have about 10 for each of many different products, so I’d like to automate the process rather than writing a line like:

ABC123.element.onclick = function() {
    selectUnselect(ABC123);
    };

for each component. I only included a few components here to give the idea.

The two commented lines of code under the noted “Problem Line” are the two things I’ve tried — my goal is to get every added component to pass its variable along to the selectUnselect function when its element (div in the html) is clicked on.

  • The first commented line results in the console.log printing out “undefined” any time one of the component divs is clicked on.
  • The second commented line prints out every instance of Component correctly, but does so automatically when the page loads, and does not respond to click events.

How can I pass the variable to a function when it’s clicked for every instance of Component?

  • 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-05-23T19:15:20+00:00Added an answer on May 23, 2026 at 7:15 pm

    The problem with your first line is that, in javascript, when a function is not called as method (myObj.myFunc()) the ‘this’ variable is not bound to the the object context the function was declared in, it is rather bound to the global object.

    In your case ‘this’ is bound to the triggering HTML element – as it is always the case with DOM events (such as onclick).

    ‘component’ should actually give you the HTML element that triggered the click.

    The problem with the second line is that you execute the function and assign the result to the onclcik handler.

    What you want to do in order to fix the first line is the following:

    that = this;
    this.element.onclick = function(){
      selectUnselect(that);
    };
    

    The reason it works is that although the function isn’t bound to ‘this’ of the object context it was declared in, all other variables of the object context the function was declared in are visible to it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

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.