Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery not getting the password field value

I have searched a lot in stackoverflow and other sites and got many solutions but nothing solved mine.That is why I am posting this.I have a password field in my web page, which was created dynamically on a link click.And I want to get the value typed in that password field into a variable when it loses focus.For that i created a jQuery function like this.

$('#parentdiv').on('focusout', '#passwordfield', function() {
      var pass1 = $('#passwordfield').val();
      alert(pass1);
      alert("Hello");
});

Here the first alert command will displayed with nothing, but the second alert come with "Hello". Which means, when the password field loses its focus this function executes without any problem, but the problem is, it is not getting the password field value.

Please help me to solve this problem.

EDIT

This is my html

<input type="password" size=20 id="passwordfield" />
like image 944
user2356932 Avatar asked May 09 '13 03:05

user2356932


People also ask

What is val() in jQuery?

jQuery val() Method The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element. When used to set value: This method sets the value of the value attribute for ALL matched elements.

What does jQuery val return?

val() returns an array containing the value of each selected option. As of jQuery 3.0, if no options are selected, it returns an empty array; prior to jQuery 3.0, it returns null .

How can we use jQuery to Get the value of an input element with id name?

Answer: Use the jQuery val() Method You can simply use the jQuery val() method to get the value in an input text box. Try out the following example by entering something in the text input box and then click the "Show Value" button, it will display the result in an alert dialog box.


2 Answers

you can try this:

HTML code:

<form id='form'>
<input type='password' class='required' />
</form>

JavaScript:

$('#form .required').focusout(function(){
var txt = $(this).val();
$('.required').after(txt);
});

or you can check this code at http://jsfiddle.net/92y6b/

like image 172
Uchit Shrestha Avatar answered Nov 05 '22 09:11

Uchit Shrestha


My solution based on above idea:

<input type="password" class="form-control" name="password" />
<script>
    $('input[name=password]').keyup(function(){
        var password = $(this).val();
    });
</script>
like image 2
Świeżu Avatar answered Nov 05 '22 08:11

Świeżu