When using PHP and I want to find out the data being posted, I simply dump the array $_POST
which will provide me with the name=
attribute of all html form elements that have been submitted input, select, textarea
etc, and their value=
attributes in an easy to read manner.
I'm wondering if Javascript or the jQuery library has a method/plugin that does the same thing for all html form elements on the page? This would be very useful for testing out client-side validation etc.
Any answers would be greatly appreciated, as this would help me save a lot of time debugging!
jQuery has the serializeArray
method. If you call it on a form, you'll get a list of objects containing name
s and value
s of fields in the form.
Yep, you can loop through all form elements on the page using jQuery like this:
$("input, textarea, select").each(function() {
alert($(this).attr("name") + " = " + $(this).val());
});
This will alert all the names and values on the page.