0

I'm trying to try a simple login page. I add a few text fields and want to read them using javascript or jquery. The issue I'm having is that I'm getting a null error every time using javascript and a blank using jquery, even if I type text into the field.

html:

<label id='userNameLabel' class='inputLabel' for='userNameInput'>Username</label>
<input id='userNameInput' name='User' class='userInput' type='text' value=''>
<label id='passwordLabel' class='inputLabel' for='passwordInput'>Password</label>
<input id='passwordInput' name='Password' class='userInput' type='text' value=''>
<input id='submitSignIn' class='button' type='submit' value='Log In'>

js:

$(document).ready(function(){
 $('#submitSignIn').click(function(){
  $userName = $('#userNameInput').text();
  $userName = $('#passwordInput').text();
  $rememberMe = $('#rememberMe').val();
 });
})

-or

$(document).ready(function(){
 $('#submitSignIn').click(function(){
  $userName = document.getElementById(userNameInput).value;
  $userName = document.getElementById(passwordInput).value;
  $rememberMe = document.getElementById(rememberMe).value;
 });
})

Can anyone tell me why I'm having this issue?

4
  • 2
    the jquery function to get an input value is .val(), not .text() Commented Aug 20, 2014 at 6:09
  • instead of using .click() you should probably use .submit() on the form Commented Aug 20, 2014 at 6:09
  • to elaborate more on my previous comment: People can submit form even without really clicking; Or you may want to submit the form programatically later, what do you know... That's why .click() on the submit button is less reliable than $("form").submit(funtion(){}); Commented Aug 20, 2014 at 6:11
  • @Lukas1 but it will work. OP: did you check your console? Does it give any errors? And in the JS #rememberMe is undefined. Check my fiddle for a working example: jsfiddle.net/GuyT/9evt6s6b Commented Aug 20, 2014 at 6:23

3 Answers 3

5
$userName = $('#userNameInput').text();

This attempts to take the child nodes of the input and extract the text from them.

Input elements can't have any child nodes. You want val().

$userName = document.getElementById(userNameInput).value;

This attempts to get the element by the id in the string variable userNameInput, which is undeclared. You need to put quotes around it to make it a string literal.

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

Comments

0

Your jQuery is using .text() rather than .val() - inputs are self-closing elements, hence do not have any text.

Your JavaScript is missing quote marks around userNameInput, meaning it is getting the variable userNameInput rather than the string "userNameInput".

Comments

-1

for check bus use is checked method

   $(document).ready(function(){
     $('#submitSignIn').click(function(){
      $userName = $('#userNameInput').val();
      $userPass = $('#passwordInput').val();
     })

;

Comments

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.