Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oliveratgithub/0700af61efdc4e54438bb1c615c63c36 to your computer and use it in GitHub Desktop.
Save oliveratgithub/0700af61efdc4e54438bb1c615c63c36 to your computer and use it in GitHub Desktop.
πŸ“‹ Store and refill HTML web form field values: a dynamic browser bookmarklet to re-fill a form with pre-defined values in 1-click. Great help for repeated testing of web forms.
javascript:(function()%7Bfunction%20copyToClipboard%20(text)%20%7B%20window.prompt%20(%22Copy%20to%20clipboard%20Using%20Ctrl+C%20and%20then%20paste%20into%20destination%20bookmarklet%22,%20text);%20%7D%20function%20addToForms(i,form)%20%7Bvar%20str%20=%20'';%20var%20obj%20=%20document.forms%5Bi%5D.querySelectorAll(%5B'textarea',%20'select',%20'input'%5D);%20for%20(var%20j=0;%20j%3Cobj.length;%20j++)%20%7B%20if%20(obj%5Bj%5D.type%20==%22checkbox%22%7C%7Cobj%5Bj%5D.type%20==%22radio%22)%20%7Bstr%20+=%20%22setVal(%22%20+%20i%20+%20%22,f,'%22%20+%20obj%5Bj%5D.name%20+%20%22',%22%20+%20obj%5Bj%5D.checked%20+%20%22);%22;%7D%20else%20%7B%20str%20+=%20%22setVal(%22%20+%20i%20+%20%22,f,'%22%20+%20obj%5Bj%5D.name%20+%20%22','%22%20+%20escape(obj%5Bj%5D.value)%20+%20%22');%22;%7D%7D%20return%20str;%7Dvar%20forms%20=%20document.forms;%20var%20str%20=%20%22%22;%20var%20f%20=%20new%20Array();for%20(var%20i=0;i%3Cforms.length;i++)%7Bf.push('%22'%20+%20forms%5Bi%5D.name%20+%20'%22'%20);%20str%20+=%20addToForms(i,forms%5Bi%5D);%7D%20copyToClipboard('javascript:(function()%7Bfunction%20setVal(i,f,field,val)%20%7Bvar%20obj%20=%20null;%20if%20(f%5Bi%5D%20&&%20f%5Bi%5D%20!=%20%22%22%20&&%20document.forms%5Bf%5Bi%5D%5D%20&&%20document.forms%5Bf%5Bi%5D%5D%5Bfield%5D)%20%7Bobj%20=%20document.forms%5Bf%5Bi%5D%5D%5Bfield%5D;%7D%20else%20if((!f%5Bi%5D%20%7C%7C%20f%5Bi%5D%20===%20%22%22)%20&&%20document.forms%5Bi%5D%20&&%20document.forms%5Bi%5D%5Bfield%5D)%20%7Bobj%20=%20document.forms%5Bi%5D%5Bfield%5D;%7D%20if%20(obj%20&&%20!obj.readOnly%20&&%20!obj.hidden)%7Bif%20(obj.type%20===%20%22checkbox%22%20%7C%7C%20obj.type%20===%20%22radio%22)%7Bobj.checked%20=%20val%7D%20else%20%7Bobj.value%20=%20unescape(val);%7D%7D%7D%20var%20f%20=%20%5B'%20+%20f.toString()%20+%20%20'%5D;'%20+%20str%20+%20%20'%7D)();');%7D)();
(function() {
function setVal(i, f, field, val) {
var obj = null;
if (f[i] && f[i] != "" && document.forms[f[i]] && document.forms[f[i]][field]) {
obj = document.forms[f[i]][field];
} else if ((!f[i] || f[i] === "") && document.forms[i] && document.forms[i][field]) {
obj = document.forms[i][field];
}
if (obj && !obj.readOnly && !obj.hidden) {
if (obj.type === "checkbox" || obj.type === "radio") {
obj.checked = val
} else {
obj.value = unescape(val);
}
}
}
var f = [""];
setVal(0, f, 'given-name', 'Abc Def');
setVal(0, f, 'family-name', 'Geh Idj');
setVal(0, f, 'dob', '1999-04-04');
setVal(0, f, 'email', 'abc@def.com');
setVal(0, f, 'url', 'https://test.it');
setVal(0, f, 'phone', '+4171234567');
setVal(0, f, 'shoes', '9');
setVal(0, f, 'skill', '1');
setVal(0, f, 'address', 'My Street 123, Apartment #0');
setVal(0, f, 'postCode', '1234');
setVal(0, f, 'country', 'Switzerland');
setVal(0, f, 'cardtype', false);
setVal(0, f, 'cardtype', false);
setVal(0, f, 'cardtype', false);
setVal(0, f, 'cardName', '');
setVal(0, f, 'cardNo', '');
setVal(0, f, 'security', '');
setVal(0, f, 'expiry', '');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment