0

How go get an input text value in JavaScript?

I want to get input text value in jquery script but it prompted empty.

my script code:

<script type="text/javascript"> 
    var emails;
    function checkRegistration() {
        emails = document.getElementById('my_email').value;
        alert(emails);
    }
</script>

my form code :

<form method="post" role="form" onSubmit="return checkRegistration()" action="#">
    <div class="form-group">
      <input type="email" class="form-control" id="my_email" name="email" placeholder="Enter a valid email address">
    </div>
    <div class="form-group">
      <input type="submit" name="submit" class="btn btn-primary btn-block" value="Forgot Password" />
    </div>
</form>
7
  • 3
    You're not using jQuery Commented Jan 19, 2016 at 16:55
  • I don't find any issues with your code, it runs fine.And to answer your title $('#my_email').val(); Commented Jan 19, 2016 at 17:05
  • @SanKrish jsfiddle.net/yo7k586b/1 does not work. It returns "Uncaught ReferenceError: checkRegistration is not defined" Commented Jan 19, 2016 at 17:08
  • And here it in jsbin jsbin.com/mifehanala/edit?html,js,console Commented Jan 19, 2016 at 17:10
  • Interesting @SanKrish - I wonder what the differences are? Commented Jan 19, 2016 at 17:18

2 Answers 2

1

First check are you using jquery.min.js file or not.

Second if you want to get value using id. id should be unique on that page.

Try below

var my_email= $('#my_email').val();
Sign up to request clarification or add additional context in comments.

3 Comments

Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO.
Why does it matter if the OP is "using jquery min file or not"? Minified or not minified won't make a difference to the answer.
@Rajender It doesnt answer the op's question , read the complete question . As your answer only solves his title
0

Edit, Updated

Add required attribute to input type="email" element, to prevent form submission if value not entered by user


Use .onchange event

var emails = document.getElementById("my_email");

function checkRegistration() {
  alert(this.value);
}

emails.onchange = checkRegistration;
<form method="post" role="form" action="#">
  <div class="form-group">
    <input type="email" class="form-control" id="my_email" name="email" placeholder="Enter a valid email address" required>
  </div>
  <div class="form-group">
    <input type="submit" name="submit" class="btn btn-primary btn-block" value="Forgot Password" />
  </div>
</form>

5 Comments

The OP is using a submit event, so why use the onchange()?
@JayBlanchard Does submit event of form element with action attribute set to # reload page ?
It does, but it will still throw the alert prior to the page reloading.
@JayBlanchard If user submits form before entering values the alert would be empty string. This is why used onchange event at Answer above. Perhaps solution is to add required attribute to input type="email" element ?
Given that the OP says "it prompted empty" you may be right.

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.