0

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);
    }
 });

5 Answers 5

4

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

<input id="hiddeninput" type="hidden" value="34345" />
Sign up to request clarification or add additional context in comments.

2 Comments

I closed the input tag. Still, the tag is re-formatted to this <input id="hiddeninput" type="hidden">34345</input> after I retrieve the value via a POST.
@Jon try changing this $("#hidden").html(resultObj.SomeNumber); to $("#hidden").val(resultObj.SomeNumber);
2

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"> 

Comments

0

$("#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.

Comments

0

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()

2 Comments

Hey, this works! But does anyone see a problem that I'm not using the "right" way to format the input tag?
Well it is malformated markup, so you should probably start doing it the right way :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.