user4820423
user4820423

Reputation: 103

JQuery .val() undefined(?) value on input

I've got a little problem with JQuery .val() used on an id of an input. I've tried many fruitless things so here I am.

I just try to get the value of the email input (what the user typed in before submition) when the user submits the form, but all I have is an empty line in the console (not even undefined value ?)

EDIT : It appears to work here, so my question is now why does this code not work ? Maybe my JQuery version is to old ?

Here is a simple version of my code, the script is at the end of the page (page system of JQuery mobile) so after the html of the concerned page.

$('#loginForm').submit(function() {

  var email = $("#email").val();
  console.log(email);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<form action="#test" id="loginForm">

  <div data-role="fieldcontain">
    <input type="email" name="email" id="email" placeholder="Email" />
  </div>
  <div data-role="fieldcontain">
    <input type="password" name="password" id="password" placeholder="Pwd" />
  </div>
  <input type="submit" id="submit" value="Login" />

</form>

Thanks a lot for your time.

Upvotes: 2

Views: 3748

Answers (2)

user4820423
user4820423

Reputation: 103

I found a solution. As strange as it may seem, the id name was the problem. I changed #email to #someswearword and it worked.

Thanks for your time, bye

Upvotes: 1

tanaydin
tanaydin

Reputation: 5306

It seems form submited when user clicks "submit" button, page changes and console gets cleaned. So you can't see value. You can alert it instead of logging to console to be sure.

Upvotes: 0

Related Questions