Manish Tiwari
Manish Tiwari

Reputation: 1866

How to get an input text value in jquery?

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>

Upvotes: 0

Views: 100

Answers (2)

Rajender Kumar
Rajender Kumar

Reputation: 166

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();

Upvotes: 1

guest271314
guest271314

Reputation: 1

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>

Upvotes: 0

Related Questions