Liam Schnell
Liam Schnell

Reputation: 482

Get text with id of textbox javascript

I know, this is just so common. However, I am not able to get it working.

Got a php file which is included in the index.php and that contains a function printing this one out

echo "<input type=text id=test name=schuelername value=Name><br>
    <button style='float: left;' onclick='update();'>Suchen</button>";

Now I want to pass this value of the textbox. With (this is the update function)

alert(window.document.getElementById("test").value);

run by an external JS file (that works, Tested it, the only problem is the value of the textbox) isn't showing up.

Argh. Why do I define the damn ID if JS can't access it (or so...)

SOLVED: Well, I ran a document.write before it and I couldn't imagine, that like this, the rest of the javascript can't run because I just deleted everything. Thanks alot, stupid me, late!

Upvotes: 0

Views: 1329

Answers (1)

SeanCannon
SeanCannon

Reputation: 77966

That should work. Try alert(window.document.getElementById("test").defaultValue);

Demo: http://jsfiddle.net/AlienWebguy/RzrZZ/

Make sure your PHP is echoing that HTML before your Javascript tries to find it and alert the value. If the actions are reversed, getElementById("test") will be undefined because the node would not have been added to the DOM yet.

Upvotes: 1

Related Questions