Getting submitted form data with JQuery

Acquiring submitted form data with JQuery has always been a bit of a problem for me so I thought I would share how I (still a novice with JQuery) now handle this problem. My first method is to loop over all the input, select and textarea's within the form.

view plain print about
1$.each($("input, select, textarea"), function(i,v) { var theElement = $(this).attr('name'); var theValue = $(this).attr('value'); if (($(this).attr('type') == 'radio')){ if ($(this).is(':checked')){ alert(theElement + ' - ' + theValue); } } else { alert(theElement + ' - ' + theValue); } });

using the attr() function we can extract the name and value of each form element. The other method is to use the serialize() function

view plain print about
1formDetails = $("#regDetails").serialize(); alert(formDetails);
Serialize is typically used to prepare user input data to be posted to a server. The serialized data is in a standard format that is compatible with almost all server side programming languages and frameworks. In order to work properly serialize requires that form fields have a name attribute. You can find a example here

TweetBacks
Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.9.5.004. Contact Blog Owner