Skip to content Skip to sidebar Skip to footer

How To Get The Specific Form Field Value Using Jquery

I have a form

Solution 1:

$("#post_comment").submit(function(event){
    var inputValue = $("input[name='type']",this).val(); 
});

Solution 2:

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

Solution 3:

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



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

Solution 4:

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

or 

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

Solution 5:

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 a Comment for "How To Get The Specific Form Field Value Using Jquery"