Getting text field value using jQuery

I am creating a login form dynamically using jQuery and AJAX answer. The form is created and displayed correctly. But I can not read the form data.

This is my code:

$(document).ready(function(){
  $('#proper-form').on( 'click', '#submit-button', function() {  // loginForm is submitted

alert ("here");
    var username = $('#email').attr('value'); // get username
    var password = $('#password').attr('value'); // get password
alert (password);
alert (username);

    // code for form submit using ajax

 }); 
});

It warns undefined undefinedfor both username and password.

Can someone help me figure out what's wrong here?

+4
source share
4 answers

Just use the function val()to get the value:

 var username = $('#email').val(); 
 var password = $('#password').val(); 

To set a value, use val(value):

$('#email').val('some new value'); 
+9
source

You need to use .val () to read the value of the input element, not the attribute

var username = $('#email').val();
+2
source

, , , .attr() , .

      $ ( '# '). ( '');, - :

    input.getAttribute('value'), 

"-" :

    <input type="text"value="something"/> 

, .

,       $('#email').val()   - : input.value, , .

= "" : , .

.val().

0
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){             
$("#test").click(function(){ 
//textbox            
        var first_name = $("#first_name").val();
        alert(first_name);
 });
 });
</script>
<div>username <input type="text" id="first_name" class="first_name" name="first_name" placeholder="type username"/>
<a href="javascript:void(0)" id="test" class="shortc-button small blue">Get Data</a></div>

. http://www.codingprogrammer.com/tutorialdemo/jquery-tutorial/get-value-of-textbox-in-jquery-example/

0

All Articles