Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

188
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda