Vivek Sadh
Vivek Sadh

Reputation: 4268

Saved user/password null on document.ready

I am trying to get username/password saved value(I saved the user/pass on my browser) when a page is loaded.

$(document).ready(function(){
    validate();
    $("input").keyup(function(){
        validate();
    });

    function validate(){
      console.info($("#username").val())
       console.info($("#password").val())

    }

but it keeps coming empty. When I start typing then only it outputs value.

Upvotes: 0

Views: 404

Answers (2)

pradeep Gavhane
pradeep Gavhane

Reputation: 25

above code works perfectly. But if you are storing value in hidden fields then you should assign value of >each hidden fields to respective HTML element.

function validate(){
  console.info($("#username").val())
  console.info($("#password").val())
}

$(document).ready(function(){
    $("#username").val($("hdnUserName").val());
    $("#password").val($("hdnPassword").val());
    $("input").keyup(function(){
        validate();
    });

    validate();
});

Upvotes: 0

Simon Deconde
Simon Deconde

Reputation: 309

You need to declare your function before calling it.

Try this code:

function validate(){
  console.info($("#username").val())
  console.info($("#password").val())
}

$(document).ready(function(){

    $("input").keyup(function(){
        validate();
    });

    validate();
  });

Here's an example: https://jsfiddle.net/qd9cr5xd/

Upvotes: 1

Related Questions