2

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.

5
  • Where is the above script in the below code in your original HTML? JQuery has to be included above all scripts that use JQuery. Commented Apr 29, 2015 at 22:04
  • 1
    .val() has been included in jQuery since version 1.0. Maybe you have another #email element on the page that is higher up in the DOM that is being selected? This can happen somewhat easily with jQuery Mobile sites. Commented Apr 29, 2015 at 22:06
  • And have you used $(document).ready? Commented Apr 29, 2015 at 22:09
  • Hi, thanks for the answers. Yes I've included JQuery above all scripts. No I just have one #email. Yes I've tried with $(document).ready Commented Apr 30, 2015 at 22:13
  • I forgot to say that since #email is my first input, the code works when I write $('input').val() but I'ld like to know why it doesn't work with #email if it works with input. Commented Apr 30, 2015 at 22:31

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

I suspect logging is not his real goal, but just using the value in the submit function.
yes console.log is working, but after submit page changes... so user4820423 can't see it on console.
Yes Barmar, logging is not my initial goal, it is just to 'debug' and see why my code doesn't work. I use the email (and password) for an authentication using btoa.
I've also tried to alert in vain. I've got an empty alert

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.