Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
Global variable used in object property not updating inside the object - javascript

I have an object set up like so:

let xVal = 0;

const myObj = {
  infoText: "The value of x is: " + xVal
}

This infoText is referenced later to display somewhat of a tooltip when hovering over an element.

function showTooltip(obj) {
  targetEl = document.getElementById('targetId');
  targetEl.innerHTML = obj.infoText;
}

The value of xVal can change. The issue I'm running into is that despite whether or not that value has changed, when I hover over my element, it will always display the initial value of xVal. I assume this is because myObj.infoText is established when it's initiated and pulls the initial value of xVal. How can I get myObj.infoText to show the current xVal and keep up with the changes?

EDIT - Here is a better example of my code, I realize I offered a poor sample originally.

let count = 0;
let clickVal = 1;

const myObj = {
  cost: 10,
  infoText: `This displays the current value of your click <br />
  Current click = ` + clickVal
};

function cardContent(obj) {
  targetEl = document.getElementById('targetId');
  targetEl.innerHTML = obj.infoText;
}
cardContent(myObj); // initialize card content

function handleClick() {
  count += clickVal;
}

function upgradeClick() {
  count -= myObj.cost;
  clickVal += 1;
  cardContent(myObj) // attempting to update the text to reflect new value
}

There is a button that invokes handleClick and one that invokes upgradeClick. Using CSS, the following div is hidden and shows when hovering over the upgradeClick button. <div id='targetId'></div> This is where the infoText shows.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use something like a getter

let xVal = 0;

const myObj = {
  get infoText() {return "The value of x is: " + xVal;}
}

function showTooltip(obj) {
  console.log(obj.infoText);
}

showTooltip(myObj);
xVal = 29;
showTooltip(myObj);
xVal = 794;
showTooltip(myObj);

about 4 years ago · Juan Pablo Isaza Report

0

Creating the object everytime inside of the showTooltip should work

let xVal = 0;

function showTooltip() {
    const myObj = {
        infoText: "The value of x is: " + xVal
    }
    console.log(myObj.infoText);
}

showTooltip();
xVal = 50;
showTooltip();

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!