Skip to content

Instantly share code, notes, and snippets.

@caseywatts
Last active October 14, 2022 00:07
Show Gist options
  • Save caseywatts/5952495 to your computer and use it in GitHub Desktop.
Save caseywatts/5952495 to your computer and use it in GitHub Desktop.
Easily fill out web forms using data from a spreadsheet using this bookmarklet.

Other gists & tricks: http://caseywatts.com/gists-and-tricks

To set this up, paste the code below into the "location" of a bookmark. Make sure it starts with "javascript:", some browsers strip this out when you paste.

  1. Find the "name" of each form element you want to put data into. You can find the "name" by right clicking on the form box and "inspecting" it.
  2. By using a spreadsheet, create a set of urls with the data you want to input. Example: variablename1 is the "name" of the form field. "value1" is the value you want to put into the box.www.website.com/page.html?variablename1=value1&variablename2=value23. Navigate to that custom URL4. Click the bookmarklet. Voila!
javascript: function getUrlVars() {
var vars = [],
hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
function ParamsIntoForms(get) {
for (var i = 0; i < get.length; i++) {
if (document.getElementsByName(get[i]).length != 0) {
document.getElementsByName(get[i])[0].value = decodeURIComponent(get[get[i]]);
}
}
return false;
}
var get = getUrlVars();
ParamsIntoForms(get);
@newfoundmagic
Copy link

Casey, this rocks! I just got this to work. Thank you!
To help the next person who looks at this and struggles with it, here are a couple tips:
--Once you've created the bookmarklet, then create the URL like Casey describes. Here's an example of one I got to work: https://www.miu.edu/application/page.html?firstname=Luke&lastname=Hillis and then run your bookmarklet. You can try it with mine, and it works!
--If you're unfamiliar with bookmarklets, check out Casey's tutorial on bookmarklets: https://gist.github.com/caseywatts/c0cec1f89ccdb8b469b1
--and for those who are visual learners, here's a video for you! I'm not a coder, so if you're not a coder either, take comfort that you'll get a non-coding explanation of what's going on. https://drive.google.com/file/d/1aUu7vubmr46tADntgTG3Cg6MLR05Gb0i/view

Enjoy, and thanks again Casey!

@kibkib
Copy link

kibkib commented Feb 8, 2021

Thanks. This is useful.
It did not work for me when there were reserved characters in the element name.

! | # | $ | & | ' | ( | ) | * | + | , | / | : | ; | = | ? | @ | [ | ]
%21 | %23 | %24 | %26 | %27 | %28 | %29 | %2A | %2B | %2C | %2F | %3A | %3B | %3D | %3F | %40 | %5B | %5D

I had to change it like this:
....

            dcd=decodeURIComponent(get[i]);
            if (document.getElementsByName(dcd).length != 0) {
                document.getElementsByName(dcd)[0].value = decodeURIComponent(get[get[i]]);
               //also I fire an onchange event of the changed element as the form i need to fill requires it
                document.getElementsByName(dcd)[0].onchange();

            }
.
```...

@psthampi
Copy link

psthampi commented Feb 26, 2021

Thanks
For text fields like first name and last name and date of birth it works fine
Is there any extra code for radio buttons in this case for sex of patient.
example for male patient i tried
ctl00$cntHolder$PatientDemographics1$rdPatientSex=M
It does not work
Thanks
ss

Any advise?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment