jQuery output all input values in a form

Share this article

Simple jQuery code snippet to output all the values of input elements within a form with a specific id. You can get both the attribute name and value this way.
console.log("----------------------------------");
$('#form-id > input,#form-id > textarea').each(function(index) {
    console.log($(this).attr('name') + " = " + $(this).val());
});
console.log("----------------------------------");
Note: the console.log() commands are just for use with firebug.

Frequently Asked Questions (FAQs) about jQuery Output Input Values Form

How can I get the value of an input text box using jQuery?

To get the value of an input text box using jQuery, you can use the .val() method. This method returns the value of the selected elements. Here’s an example:

$(document).ready(function(){
$("button").click(function(){
alert("Value: " + $("#test").val());
});
});
In this example, when the button is clicked, the value of the input element with id “test” is alerted.

How can I set the value of an input text box using jQuery?

To set the value of an input text box using jQuery, you can also use the .val() method. When this method is used to set a value, it sets the value of ALL matched elements. Here’s an example:

$(document).ready(function(){
$("button").click(function(){
$("#test").val("Hello World");
});
});
In this example, when the button is clicked, the value of the input element with id “test” is set to “Hello World”.

How can I get all input values from a specific form using jQuery?

To get all input values from a specific form, you can use the .serialize() method. This method creates a URL encoded text string by serializing form values. Here’s an example:

$(document).ready(function(){
$("button").click(function(){
alert($("form").serialize());
});
});
In this example, when the button is clicked, the values of all elements in the form are alerted in a URL encoded text string.

How can I console log input values using jQuery?

To console log input values, you can use the console.log() method along with the .val() method. Here’s an example:

$(document).ready(function(){
$("button").click(function(){
console.log($("#test").val());
});
});
In this example, when the button is clicked, the value of the input element with id “test” is logged in the console.

How can I get the value of a selected option in a dropdown list using jQuery?

To get the value of a selected option in a dropdown list, you can use the .val() method. Here’s an example:

$(document).ready(function(){
$("button").click(function(){
alert($("#mySelect").val());
});
});
In this example, when the button is clicked, the value of the selected option in the dropdown list with id “mySelect” is alerted.

How can I set the value of a selected option in a dropdown list using jQuery?

To set the value of a selected option in a dropdown list, you can also use the .val() method. Here’s an example:

$(document).ready(function(){
$("button").click(function(){
$("#mySelect").val("Option2");
});
});
In this example, when the button is clicked, the value of the selected option in the dropdown list with id “mySelect” is set to “Option2”.

How can I get the value of a checkbox using jQuery?

To get the value of a checkbox, you can use the .prop() method along with the .val() method. Here’s an example:

$(document).ready(function(){
$("button").click(function(){
if($("#myCheck").prop("checked")){
alert($("#myCheck").val());
}
});
});
In this example, when the button is clicked, if the checkbox with id “myCheck” is checked, its value is alerted.

How can I set the value of a checkbox using jQuery?

To set the value of a checkbox, you can use the .val() method. Here’s an example:

$(document).ready(function(){
$("button").click(function(){
$("#myCheck").val("Checked");
});
});
In this example, when the button is clicked, the value of the checkbox with id “myCheck” is set to “Checked”.

How can I get the value of a radio button using jQuery?

To get the value of a radio button, you can use the :checked selector along with the .val() method. Here’s an example:

$(document).ready(function(){
$("button").click(function(){
alert($("input[name='myRadio']:checked").val());
});
});
In this example, when the button is clicked, the value of the checked radio button with name “myRadio” is alerted.

How can I set the value of a radio button using jQuery?

To set the value of a radio button, you can use the .prop() method. Here’s an example:

$(document).ready(function(){
$("button").click(function(){
$("input[name='myRadio']").prop("checked", true);
});
});
In this example, when the button is clicked, the radio button with name “myRadio” is checked.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week