This is what I use to include/exclude each of the elements as I need them from the form. This method also makes our older forms serializable even though some of the elements only have id's defined and not names.
$( 'textarea' ).each( function() {
$(this).attr( 'type', 'textarea' );
});
$( 'input:text:not( ".excluded" ), input:checkbox, input:radio, textarea' ).each( function() {
if (!$(this).hasClass( 'answer' )) {
$(this).addClass( 'answer' );
}
if ( !$(this).attr( "name" ) && $(this).attr( 'id' ) ) {
$(this).attr( "name", $(this).attr("id") );
}
});
Then I call the function below to get my serialized array on the $( '.answer' ).change()
event, on page navigation and on the $('form').submit()
event. This method puts no noticeable load on the page performance that I can discern.
function storeFormData() {
var serializedData = $( ".answer" ).serializeArray();
var formDataObj = serializedData;
var formDataString = JSON.stringify(formDataObj);
localStorage.setItem(fso_id, formDataString);
return formDataString;
}
<textarea>
is being left out?myForm.clone()
) before sending it, which blanks out the textareas. It's a known - and annoying - bug: bugs.jquery.com/ticket/3016