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 1 comment

[center][/center]

Somebody # 24/05/23 10:24



Somebody # 20/09/24 01:30

 

Add Comment