0

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"/>
  • 1
    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
  • 1
    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](https://developer.mozilla.org/en-US/learn/javascript) – Jay Harris Oct 27 '13 at 19:46
  • possible duplicate of [How to get text box value in javascript](http://stackoverflow.com/questions/763745/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
  • What does "accessed from a different file" mean in this case? – mplungjan Oct 27 '13 at 19:55

2 Answers2

1

HTML

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

JavaScript

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

Try it out here.

Tim S
  • 2,309
  • 16
  • 23
0

To get the text of an input box with Javascript :

document.getElementById('textbox_id').value

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

Also see this answer.

Community
  • 1
  • 1
  • @mplungjan Yes, for example : ``. 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