Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9075263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:49:39+00:00 2026-06-16T18:49:39+00:00

This is my first post, anyway I’ll get straight to the point. I have

  • 0

This is my first post, anyway I’ll get straight to the point.

I have a form that when a user writes they’re name in, Javascript saves the value to the global variable “ch_name”. When I access this variable through an alert at any point on the site it always shows the correct value that I entered. However, it only works for alerts, when I try reference the user by using the variable it always shows as undefined.

I’ve tried re-ordering the code in case it hadn’t loaded correctly and this hasn’t yielded any results either. I’ve begun wondering whether I’ve referenced it right at all as I’m still fairly new to JavaScript.

As you’ll probably find out my code is a mess but here’s what I’ve got.

var ch_name; //my global variable

var data = new Array(); //I've taken a snippet out of an array of 8.
data[0] = '<p>Question 1</p>' + ch_name + "\n" + //ch_name is referenced on this line.
'<button onclick="results1()">Click Me</button>' + "\n" + 
'<button onclick="results2()">Click Me</button>' + "\n" +
'<button onclick="results3()">Click Me</button>';

function charName(form) { //This is the code that changes the global variable ch_name.
ch_name = document.nameform.user.value;
ranData = Math.floor(Math.random() * data.length);
document.getElementById("placeholder").innerHTML = data[ranData];
}

The html snippet:

<form NAME="nameform" method="GET">
<p>Enter your character name:</p> <input type="text" name="user" value="" />
    <input type="button" value="Start" onClick="charName(this.form)"/>
</form>
<div id="placeholder"></div>

Referencing the ch_name through an alert anywhere gives me the correct result, blank on page load and after the string is entered it gives the correct name. But anytime I reference it through the array variable it shows an undefined. This is not the full code but I’m certain there’s no conflicts in variable names etc. I’ve scanned it a good few times, I’m certain it’s just me being an idiot.

Is there a work around for this? I’m in a place ready to drop the whole code behind ch_name entirely to focus on other areas of the page.

Thanks for reading and any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:49:40+00:00Added an answer on June 16, 2026 at 6:49 pm

    The data won’t change the way you seem to think.

    I think for you the best bet would be to replace data Array with a function.

    Now the ch_name parameter will be created dynamically every time you run the function. i.e. every time the button is pressed, which will update the data.

    How do you change the data in a page?

    var ch_name; //my global variable
    function charName(form) { //This is the code that changes the global variable ch_name.
        ch_name = document.nameform.user.value;
        ranData = Math.floor(Math.random() * data.length);
        document.getElementById("placeholder").innerHTML = getranData(ranData);
     }
    
    function getranData(num){
       if(num==0)
              return '<p>Question 1</p>' + ch_name + "\n" + //ch_name is referenced on this line.
              '<button onclick="results1()">Click Me</button>' + "\n" + 
              '<button onclick="results2()">Click Me</button>' + "\n" +
              '<button onclick="results3()">Click Me</button>';
      else if (num==1) ; ///...and so on
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first post on this website, Anyway, I recently started trying to
This is my first post and I my first experience with jquery. I have
This is my first post here. I have a problem. I need to take
This is my first post here, o let me say hi :) I have
Probably a newbie question but I'll ask anyway. Have a multipage form that I
First of all... Sorry for this post. I know that there are many many
This is my first post ever :). I have quite a bit of experience
Wow, my first Stackoverflow post, I've been watching this site for ages .. anyway,
This is my first post in this forum, so please, be patient with me.
This is my first post on stackoverflow, so please excuse me if my question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.