Odyssey
Odyssey

Reputation: 263

Add text to JavaScript via input tag

This is kind of unusual, but is there anyway to add text into JavaScript via the input tag?

For instance,

 <input type="text" id="example" />

and let's say the JavaScript is

function show_alert()
{
alert("The text entered in #example will show up here");
}

Is this anyway possible?

Upvotes: 0

Views: 340

Answers (1)

nrabinowitz
nrabinowitz

Reputation: 55688

In straight Javascript (i.e. no libraries), you can use:

var exampleValue = document.getElementById('example').value;

Working example: http://jsfiddle.net/nrabinowitz/H6bKL/

But if you're going to do much of this, and especially if you want to get values from a variety of form elements, you'd do yourself a favor using jQuery or another library with good DOM access functions.

Upvotes: 2

Related Questions