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

187
Visualizações
JavaScript variable coming up as undefined when used within textNode
let state = {}
var Playername;

At the start of the code the var Playername is created.


  if (nextTextNodeId == 60) {
      var InputName = document.getElementById('name').value;
      let Playername = InputName;
      alert(Playername);
      
      if (Playername == "Andrew") {
          ToggleInputOff()
          state = Object.assign(state, option.setState)
          showTextNode(4)
          return;
      }
      else if (Playername == "") {
          state = Object.assign(state, option.setState)
          showTextNode(3)
          return; 
      }
      else {
          ToggleInputOff()
          state = Object.assign(state, option.setState)
          showTextNode(5)
          return;
      }
      return ToggleInputOff()
  }

The alert Playername displays the inputted name in the alert box correctly, however when i go to use this variable later in the project it comes up as undefined.

{
        id: 5,
        text: "Welcome " + Playername + " let us start the day!",
        options: [
            {
                text: "Let's go",
                nextText: 6
            }

        ]
    },

The text nodes are accessed through this function and displayed on the screen

function showTextNode(textNodeIndex) {
  const textNode = textNodes.find(textNode => textNode.id === textNodeIndex)
  textElement.innerText = textNode.text
  while (optionButtonsElement.firstChild) {
    optionButtonsElement.removeChild(optionButtonsElement.firstChild)
  }

  textNode.options.forEach(option => {
    if (showOption(option)) {
      const button = document.createElement('button')
      button.innerText = option.text
      button.classList.add('btn')
      button.addEventListener('click', () => selectOption(option))
      optionButtonsElement.appendChild(button)
    }
  })
}

Whenever this text node is accessed the output is

Welcome Undefined let us start the day!
about 4 years ago · Juan Pablo Isaza
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