Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda