user2693905
user2693905

Reputation:

Unable to get the value of text field in jquery

This is simple even though i don't know why i don't get it to work. I just need the value of the text field. Here is the code. http://jsfiddle.net/VL8hr/

$(document).ready(function() {

   var firstname = $('.firstname').val();
   alert(firstname);

}); 

Upvotes: 2

Views: 99

Answers (4)

Ashis Kumar
Ashis Kumar

Reputation: 6554

You selector is wrong. You have used id attribute, but in your javascript code you tends to use it as a class. It should be like this

$(document).ready(function() {

   var firstname = $('#firstname').val();
   alert(firstname);

}); 

you can differentiate between id and class selector over here.

http://api.jquery.com/id-selector/

http://api.jquery.com/class-selector/

Upvotes: 1

Biker John
Biker John

Reputation: 2711

# is selector for id and . is selector for class.

$(document).ready(function() {

       var firstname = $('#firstname').val();
       alert(firstname);

    }); 

Upvotes: 1

Karthik Ar
Karthik Ar

Reputation: 125

Change your jquery

$(document).ready(function() {

   var firstname = $('#firstname').val();
   alert(firstname);

});

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

You need to use id-selector instead of class-selector since firstname is the id of the element

var firstname = $('#firstname').val();

Demo: Fiddle

Upvotes: 3

Related Questions