Zaonce...
loading...

This Planet is a Tedious Place...

04/03/16 Remembering a partially filled form

Thanks to HTML5, here's a handy snippet of jQuery which will store unsubmitted form data (text fields, texatareas and selects) so that you don't lose everything on a page refresh or when accidentally closing a window. It's also handy if you need to repeatedly submit a form with the same data.

$( "input[type=text], textarea, select" ).each( function()
{
var id = $( this ).prop( "id" ) + "-store";
var previous = localStorage.getItem( id );
if( previous !== null )
{
$( this ).val( previous );
}
$( this ).change( function()
{
localStorage.setItem( id, $( this ).val() );
} );
} );

This can be extended to checkboxes too, but it's slightly different:

$( "input[type=checkbox]" ).each( function()
{
var id = $( this ).prop( "id" ) + "-store";
var previous = localStorage.getItem( id );
if( previous !== null )
{
$( this ).prop( "checked", previous === "true" );
}
$( this ).change( function()
{
localStorage.setItem( id, $( this ).prop( "checked" ) );
} );
} );

posted by Gruntfuggly # 16:48 Comments...


Somebody # 19/10/17 06:09

 

Add Comment