I have a form
<form id="post_ment" 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_ment").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.
I have a form
<form id="post_ment" 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_ment").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 try var a=$("input:hidden").val(); – Mahesh Patidar Commented Apr 22, 2013 at 9:52
6 Answers
Reset to default 5$("#post_ment").submit(function(event){
var inputValue = $("input[name='type']",this).val();
});
Try using an id like this:
<form id="post_ment" action="cmt.php" method="post">
<input type="hidden" id='hidden' name="type" value="sub" />
<textarea id="body"></textarea>
</form>
and later:
$("#post_ment").submit(function(event){
var hiddenValue = $("#hidden").val();
});
<form id="post_ment" 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_ment .hidden").val();
//alert(hiddenVal);
});
var form = $(this);
var inputValue = form.find('input[name="type"]').val();
or
var form = $(this);
var inputValue = form.find('input:hidden').val();
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.
$("#post_ment").submit(function(event){
var form = $("input[name='type']").val();
})