Skip to content Skip to sidebar Skip to footer

Retrieving Text Field Value Using Javascript

I want to retrieve textfield value using javascript. suppose i have a code like: And I want to retrieve it using javascript. I call a functio

Solution 1:

You can do this:

Markup:

<inputtype="text" name="txt"id="txt"/>
<inputtype="button" onclick="retrieve('txt');"/>

JavaScript:

functionretrieve(id) {
    var txtbox = document.getElementById(id);
    var value = txtbox.value;
}

Solution 2:

Let's say you have an input on your page with an id of input1, like this:

<inputtype="text"id="input1" />

You first need to get the element, and if you know the Id, you can use document.getElementById('input1'). Then, just call .value to get the value of the input box:

var value = document.getElementById('input1').value;

Update

Based on your markup, I would suggest specifying an id for your text box. Incase you don't have control over the markup, you can use document.getElementsByName, like so:

var value = document.getElementsByName('txt')[0].value;

Solution 3:

One of the way is already explained by Andrew Hare.

You can also do it by entering the value in the textbox and getting a prompt box with entered message when a user click the button.

Let's say, you have a textbox and a input button

<inputtype="text" name="myText" size="20" />
<inputtype="button" value="Alert Text" onclick="retrieve()" />  

The function for retrieve()

functionretrieve()
{
     var text = document.simpleForm.myText.value;
     alert(text);
}

Post a Comment for "Retrieving Text Field Value Using Javascript"