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

186
Views
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
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!