Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting textbox's value using jQuery

Tags:

jquery

textbox

I am creating a login form dynamically, using jQuery and AJAX response. Form is created and displaying properly. But, I'm not able to 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's alerting undefined undefined for both username and password.

Can anyone help me in figuring out what's wrong here?

like image 747
Devesh Agrawal Avatar asked Dec 19 '22 23:12

Devesh Agrawal


2 Answers

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

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

To set the value use val(value) function:

$('#email').val('some new value'); 
like image 117
Suresh Atta Avatar answered Jan 11 '23 07:01

Suresh Atta


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

var username = $('#email').val();
like image 25
Arun P Johny Avatar answered Jan 11 '23 06:01

Arun P Johny