3

I have a form

<form id="post_comment" action="cmt.php" method="post">
   <input type="hidden" name="type" value="sub" />
   <textarea id="body"></textarea>
</form>

I am accessing the form using this code

$("#post_comment").submit(function(event){

    var form = $(this);

});

How can I get the value of <input type="hidden" name="type" value="sub" /> from this form. I tried to get using form.input("type") but it is not working.

1
  • 1
    try var a=$("input:hidden").val(); Commented Apr 22, 2013 at 9:52

6 Answers 6

5
$("#post_comment").submit(function(event){
    var inputValue = $("input[name='type']",this).val(); 
});
Sign up to request clarification or add additional context in comments.

Comments

4

Try using an id like this:

<form id="post_comment" action="cmt.php" method="post">
 <input type="hidden" id='hidden' name="type" value="sub" />
 <textarea id="body"></textarea>
</form>

and later:

$("#post_comment").submit(function(event){
 var hiddenValue = $("#hidden").val(); 
});

Comments

3
<form id="post_comment" action="" method="post">
 <input type="hidden" class="hidden" name="type" value="sub" />
 <textarea id="body"></textarea>
 <input type="submit" value="submit" class="submit"/>
</form>



 $(".submit").click(function(){
   var hiddenVal=jQuery("#post_comment .hidden").val();
   //alert(hiddenVal);
 });

Comments

2
var form = $(this);
var inputValue =  form.find('input[name="type"]').val();

or 

var form = $(this);
var inputValue =  form.find('input:hidden').val();

Comments

2

Another approach for this
Consider if you have multiple forms with multiple input fields having name attribute than this code will be helpful for you :

$("#formId input[name='valueOfNameAttribute']").val()
$("#formId textarea[name='message']").val()


Hope it'll help somebody.

Comments

0
 $("#post_comment").submit(function(event){

        var form = $("input[name='type']").val();

    })

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.