1

I want to get value of input element using javaScript. I am new to all this Please help me.

5 Answers 5

6

You can use

var stringVal = document.getElementById('youInputID').value;
Sign up to request clarification or add additional context in comments.

Comments

3

Other users have answered the question above already. I would also like to recommend looking at one of the javascript libraries. They make this kind of work much easier. My current favorite is jquery. It is amazingly powerful - every day I find some new feature or trick that makes javascript programming easier.

To solve this in jquery you can use:

The line below will create an alert box for the value of an input tag with the id of email using jquery:

alert($("#email").val());

Here is a complete example:

<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
      $(document).ready(function(){
        alert($("#email").val());
      });
    </script>
  </head>
  <body>
    <form method="POST" action="go.php" id="login_form">
      <input type="text" class="inputtext" title="Email" id="email" name="email" value="your@emailaddress" />
    </form>
  </body>
</html>

1 Comment

+1 Using jQuery straight away will definitely save frustration with all the low level plumbing code. I believe that getting the value from the input element is just one of the many problems that the asker have.
2

Your Javascript should look similar to this:

var elm = document.getElementById('myElement');
var value = elm.value;

And your HTML:

<input type="text" id="myElement" value="My Value" />

Comments

1

Try this:

<input type="text" id="testid" value="" />

Now you can get the value of above text box like this:

document.getElementById('testid').value;

Other way:

document.form_name_here.element_name.value;

Simply run an alert to check if value comes:

alert(document.getElementById('testid').value);

Comments

0
var txt = document.getElementById('ID-OF-FIELD');
var value = txt.value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.