I can get the element like this $("#txtEmail")
but I'm not sure how to get the actual value.

- 37,978
- 12
- 114
- 156

- 111,873
- 86
- 233
- 325
-
23did u try $("txtEmail").val() – Perpetualcoder Jan 20 '09 at 23:20
-
2hehehe...the problem was that I forgot to reference my function in the click event =) – Micah Jan 20 '09 at 23:24
9 Answers
There's a .val()
method:
If you've got an input with an id of txtEmail
you can use the following code to access the value of the text box:
$("#txtEmail").val()
You can also use the val(string)
method to set that value:
$("#txtEmail").val("something")

- 2,209
- 8
- 24
- 31

- 8,138
- 2
- 30
- 30
-
3Why does '.value()' not work in jQuery? I wouldn't have searched Google if it did, because this is what I tried intuitively. Why choose to shorten it two chars... – Mike de Klerk Jul 26 '15 at 09:40
-
Use the .val() method.
Also I think you meant to use $("#txtEmail")
as $("txtEmail")
returns elements of type <txtEmail>
which you probably don't have.
See here at the jQuery documentation.
Also jQuery val() method.

- 34,865
- 12
- 85
- 147
Possible Duplicate:
Just Additional Info which took me long time to find.what if you were using the field name and not id for identifying the form field. You do it like this:
For radio button:
var inp= $('input:radio[name=PatientPreviouslyReceivedDrug]:checked').val();
For textbox:
var txt=$('input:text[name=DrugDurationLength]').val();

- 3,722
- 4
- 31
- 34
-
Be careful when posting copy and paste boilerplate/verbatim answers to multiple questions, these tend to be flagged as "spammy" by the community. If you're doing this then it usually means the questions are duplicates so flag them as such instead: http://stackoverflow.com/questions/1320088 – Kev Mar 08 '12 at 23:55
-
1@Kev: I did'nt knew about such tagging. However, I am not sure if it was a duplicate at all during the time I had posted this solution. I couldn't find one in SO so posted. But yes going forward i will keep in mind and thanks for letting know. – Yoosaf Abdulla Apr 16 '12 at 17:17
-
1The "input:text" helped because I had 2 controls with the same name. :< (Note my code, just inherited it) – granadaCoder Jan 13 '16 at 20:35
Noticed your comment about using it for email validation and needing a plugin, the validation plugin may help you, its located at http://bassistance.de/jquery-plugins/jquery-plugin-validation/, it comes with a e-mail rule as well.

- 163
- 1
- 8
There is a .val();
method that you can use.
So in your situation you would want to use $("#txtEmail").val();
. Also, make sure you add the id property into your html code!

- 5,509
- 14
- 55
- 90
Use the .val()
method to get the actual value of the element you need.

- 46,730
- 8
- 72
- 95

- 199
- 2
- 12
You can access the value of Texbox control either by its ID or by its Class name.
Here is the example code:
<input type="text" id="txtEmail" class="textbox" value="1">
$(document).ready(function(){
alert($("#txtEmail").val());
alert($(".textbox").val());//Not recommended
});
Using class name for getting any text-box control value can return other or wrong value as same class name can also be defined for any other control. So getting value of a specific textbox can be fetch by its id.

- 613
- 1
- 8
- 16
Per Jquery docs
The .val() method is primarily used to get the values of form elements such as input, select and textarea. When called on an empty collection, it returns undefined.
In order to retrieve the value store in the text box with id txtEmail, you can use
$("#txtEmail").val()

- 2,411
- 20
- 32

- 129
- 5