Naweed Chougle
Naweed Chougle

Reputation: 520

Retrieving hidden input value using jQuery

I have a hidden input in the manner below:

<div id="message">
    <input id="hiddeninput" type="hidden">
    <span>Message with submit button <input type=button id="confirm" value="Submit"></span>  
</div>

The hidden input is given a value after a jQuery POST. I need to retrieve the value that is set, and send it in another jQuery POST.

Interestingly, I get this:

<input id="hiddeninput" type="hidden">34345</input>

after fetching the value from the server in the first jQuery post.

Just $("#hiddeninput").val() does not retrieve the value which I want to send.

What is the correct way to do it in my example?

EDIT: In JQuery, This is how I set the value to the hidden field:

$.post("post.php", function(data){


    if(data.length > 0){
    var resultObj =  eval(data)[0];
    if(resultObj.SomeNumber >= 0)
    {
        $("#hidden").html(resultObj.SomeNumber);
    }
 });

Upvotes: 0

Views: 4339

Answers (5)

James Lelyveld
James Lelyveld

Reputation: 294

try explicitly adding the value tag to the input elemeent before the post adds it i.e.

<input id="hiddeninput" type="hidden" value="">

If that doeesn't work have a look at this thread: jquery selector can't read from hidden field

Upvotes: 0

Martin Jespersen
Martin Jespersen

Reputation: 26183

Since <input id="hiddeninput" type="hidden">34345</input> is not the "right" way to format the input tag,a s opposed to <input id="hiddeninput" type="hidden" value="34345"/> you need to use $("#hiddeninput").text()

Upvotes: 0

Mark Robinson
Mark Robinson

Reputation: 13278

$("#hiddeninput").html() would retrieve 34345 from the structure as you show although as stated above the value attribute should be used on a hidden field and then val() will work.

Upvotes: 0

BvdVen
BvdVen

Reputation: 2961

You have to set the value of the hidden field like this, then it should work

<input id="hiddeninput" type="hidden" value="34345" />

Upvotes: 4

Zaje
Zaje

Reputation: 2309

The hidden element

<input id="hiddeninput" type="hidden">

does not have the value attribute. It should be like

<input id="hiddeninput" type="hidden" value="someValue"> 

Upvotes: 2

Related Questions