Code Junkie
Code Junkie

Reputation: 7788

jQuery's val() is not working on a hidden field

I have a hidden field in my page like so:

<hidden id="tsDaySchedule01" value="7.50"></hidden>

When I try to access it with the following code, the alert returns blank:

alert($("#tsDaySchedule01").val());

Now when I use attr("value") like below, it works without issue:

alert($("#tsDaySchedule01").attr("value"));

Lastly, I would like to point out we have other non-hidden text fields within the page that work without issue using val().

I would like to have a better understanding as for what is going on here. Does anybody have an explanation?

Upvotes: 2

Views: 2687

Answers (3)

Pascalz
Pascalz

Reputation: 2378

Maybe you need to use :

<input type='hidden' id="tsDaySchedule01" value="7.50">

Upvotes: 1

.val() method only works with text-box type of element input and textarea elements.

you should use

<input type='hidden' id="tsDaySchedule01" value="7.50">

Upvotes: 1

James Donnelly
James Donnelly

Reputation: 128791

<hidden/> isn't a valid HTML element. If you're wanting a hidden input you'd use:

<input type="hidden" />

jQuery's .val() method only works on input, select and textarea elements. To get this to work for you, change your <hidden/> element to:

<input type="hidden" id="tsDaySchedule01" value="7.50" />

Upvotes: 8

Related Questions