Stack Overfolow
Stack Overfolow

Reputation: 671

jQuery get value of generated hidden input

I cannot figure it out how can i get the value of a hidden input which has been appended before.I mean i have something like this:

$(document).ready(function () {
    $(".form").append('<input type="hidden" id="inputhidden1" value="myvalue"> ');
    $(".form").submit(function () {
        console.debug($("#inputhidden1").val())
    });
});

The following code, when the form is submited will debug me the value as undefined. Why?

Upvotes: 0

Views: 406

Answers (2)

Lokendra Shekhawat
Lokendra Shekhawat

Reputation: 1

there is an error in your code, you are not using document.ready correctly and you are using double quote inside double quote. Is your form has a class named "form"?

Give your form an id, lets say you gave

<form id="formid" action="#">
       <input type="submit" value="Click Me"/>
</form>

now try this code in script:

<script type="text/javascript">

    $(document).ready(function(){
          $("#formid").append("<input type='hidden' id='inputhidden1' value='myvalue'/>");
          $("#formid").submit(function(){
              console.debug($("#inputhidden1").val());
          });
    });
</script>

its printing the value = myvalue on debug console of firebug..

Upvotes: 0

Sibu
Sibu

Reputation: 4617

Your script has syntax errors, try this way

$(document).ready(function(){// missing function
     $(".form").append("<input type='hidden' id='inputhidden1' value='myvalue'>");//missing closing tag, quotes error
     $(".form").submit(function()
        {
            console.debug($("#inputhidden1").val())
        });
});​

Working DEMO

Upvotes: 1

Related Questions