1

i am trying to use a form to call a variable from an html text box in javascript.
my html looks like this:

<script type='javascript'>
    var X = 5;
</script>
<input type='text' name='name'>
<input type='button' onclick='document.getElementById("output").innerHTML = name.value' value='click'>
<p id='output'></p>

The problem is that when you click the button the paragraph shows what you type into the text input. It seems that the script is receiving the value of name.value as a string. how can I work around this so that the paragraph shows 5 when the text box is X?

4
  • You directly assign name.value as the value, how exactly you expect it to give you X? Magic? Looks like you didn't give it any thought before asking here. Commented Nov 13, 2011 at 9:32
  • @ShadowWizard Or you didn't look very well. X is a variable with value 5. ANyway, I don't know if it's possible in Javascript to get a variable's value from its name. Commented Nov 13, 2011 at 9:35
  • 1
    @Robinj I think 'Shadow Wizard' said that because the question isn't very clear. It seems like the asker possibly wants X's value. Commented Nov 13, 2011 at 9:45
  • @RobinJ this might be, in such case Wim's answer is the correct way. Commented Nov 13, 2011 at 9:54

3 Answers 3

0

1) all "value" is always going to be a String. If you want to convert it to something else you need to do that manually.

2) you're not passing in X, you're passing in "name.value". In this situation name is a variable of some sort (not in your example). You probably want to get the value of the input field "name" in which case you'll want to give your name input an id ('nameInput' for example) and then get its value thusly:

document.getElementById('nameInput').value;  //(your html would be <input id='nameInput' type='text' etc. etc. /> 

3) if you really do want to set the value of the input to 5 you'll want to try :

document.getElementById("output").innerHTML = X
Sign up to request clarification or add additional context in comments.

2 Comments

if i were to use "document.getElementById("output").innerHTML = X" then the txtbox would not be used. i'm just trying to use the txtbox to call a variable from a javascript file.
your question is unclear. What are you actually trying to accomplish? It looks like you're trying to get the value out of the text box A and put it into text box B. If that's the case then solution 2 is what you need (in my advice above). In your code you put X as your value, so, I was helping you understand why you're seeing what you're seeing, though X seems arbitrary and irrelevant.
0

Try to replace name.value with: window[name.value]

1 Comment

Indeed, looks like that's what the OP is really after.
0

Well, I don't think there's a way to get the value from a variable when the variable is being presented as a string. Seems this is the easiest way to do it:

<input type='text' name='name'>
<input type='button' onclick='document.getElementById("output").innerHTML = ((name.value == "X") ? ("5") : (name.value))' value='click'>
<p id='output'></p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.