Take the 2-minute tour ×

I'm making a html JAVASCRIPT turtle website, how would I be able to make text from a text input box a variable and accessible from javascript? And I want it so when I change the text inside the text box that when I click OK it send the new text and not the old one.

<input type="text" value="Put a number here"/>
share|improve this question

closed as off-topic by j08691, mplungjan, Hobo Sapiens, HansUp, Ahmed Siouani Oct 28 '13 at 9:34


  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – j08691, Hobo Sapiens, HansUp, Ahmed Siouani

Show some code and perhaps read a tutorial for beginners? –  mplungjan Oct 27 '13 at 19:44
@mplungjan I have searched online, I will put some code up in a sec. –  user2344757 Oct 27 '13 at 19:45
lol you forgot to add "I have not attempted any code, but i was hoping SO would do all the work". Go to a javascript tutorial my friend, MDN –  Jay Harris Oct 27 '13 at 19:46
possible duplicate of How to get text box value in javascript - please pay attention to the suggestions given by SO when you ask. You must have ignored them all –  mplungjan Oct 27 '13 at 19:46
Sorry, none of them seemed to work. and also it's being accessed from a different file, that also is creating bugs, I have looked in the console of Google Chrome but that didn't help.... –  user2344757 Oct 27 '13 at 19:52

2 Answers 2

up vote 1 down vote accepted


<input type="text" id="theTextBox"/>
<input type="button" id="theButton" value="Ok" onclick="theButtonClick()"/>


function theButtonClick()
    var theVariable = document.getElementById("theTextBox").value;

Try it out here.

share|improve this answer
Thank you so much that worked like a treat! –  user2344757 Oct 27 '13 at 20:02
Glad I could be of help. –  Tim S Oct 27 '13 at 20:03

To get the text of an input box with Javascript :


Your input box ID should match the ID in the "getElementById()" function in your Javascript code.

Also see this answer.

share|improve this answer
@mplungjan Yes, for example : <input name="somename" id="textbox_id" type="text">. This is because your Javascript code needs a way to uniquely identify your text box, and you should give it an ID and use that same ID in your Javascript code. Yeah, of course you can replace "textbox_id" by anything you like as long as it's the same in the input field and in the Javascript. –  Noel Stack Oct 27 '13 at 19:52
@mplungjan I edited my answer, hopefully it's more clear now. –  Noel Stack Oct 27 '13 at 19:58
Yes, that was much better. –  mplungjan Oct 27 '13 at 19:59
