tags:

views:

60

answers:

5

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

+6  A: 

You can use

var stringVal = document.getElementById('youInputID').value;
astander
A: 
var txt = document.getElementById('ID-OF-FIELD');
var value = txt.value;
Ritz
+2  A: 

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" />
eyazici
+1  A: 

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);
Sarfraz
+3  A: 

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>
SnapShot
+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.
Nordin