Skip to content

Instantly share code, notes, and snippets.

@kopiro
Created June 19, 2012 17:14
Show Gist options
  • Save kopiro/2955360 to your computer and use it in GitHub Desktop.
Save kopiro/2955360 to your computer and use it in GitHub Desktop.
Autocomplete Forms (test)
var fields = {
'nome':'Flavio',
'cognome':'De Stefano',
'sesso':'m',
'e-mail':'[email protected]',
'email':'[email protected]'
};
function sortfunction(a, b)
{
return (a.c < b.c);
}
var liked = function(t)
{
console.log('LIKED:', t);
var matches = [];
for (var k in fields)
{
var c = t.match( new RegExp(k, 'i') );
if (c)
{
var r = fields[k];
if (r)
matches.push({ r:r, c:c[0].length });
}
}
if (matches)
{
matches = matches.sort(sortfunction);
if (matches[0]) return matches[0].r;
}
}
var go = function()
{
$('form input[type=text]:visible, form input[type=email]:visible').each(function(k,v)
{
var label = $('form label')[k];
var labeltext = $(label).text();
var toi = liked(labeltext);
console.log('TOI', toi);
$(v).val( toi )
});
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment