Help me solve my seems-like-it-should-be-easy-but-I-can't-figure-it-out JavaScript/Greasemonkey problem!
OK, Here goes: I have a list of many thousands of ID numbers that refer to records in a database. I need to submit each one to a form in the URL (i.e. form.php?id=12345), change the value of one measly pulldown menu on the form, and then submit it. I can't do anything spiffy with AJAX or modify the form somehow, because it's hosted/controlled by another entity. It seemed stupid to me to do these all by hand, so I thought I would try to write a Greasemonkey script that would loop through each ID number, load the form in an IFRAME, change the value, and submit the form. It seemed simple enough at the time...
To test it out, I made a basic mock-up page that loads a form in an IFRAME with the same relevant form information and field name, and then tried to write a script that would change the form. The problem is, nothing works! I know there are a lot of different syntax and browser considerations for how to refer to different objects, and I've pored over dozens of code examples but am clearly still missing something. I can get the script to acknowledge the existence of the form using getElementsByTagName(), but I can't get it to do anything with the form or the SELECT element in it. It always either doesn't find anything at all, or it says the object has no properties. It seems like it should only require a few lines of code to:
1. Find the SELECT object with the name "right.a" in the form that's inside the IFRAME with the id "myframe"
2. Change the value and selectedIndex of that form item
3. Submit the form
But I can't get the element by name, nor can I get it through an array (i.e. forms[0]). Using contentDocument to get to what's inside the frame after I've changed the src seems to be a step in the right direction, but I can't get any positive result after that.
The relevant pages are here:
The test mock-up
The form
The Greasemonkey script
And the source code snippets are
here.
Any insight would be much, much appreciated so I don't give myself carpal tunnel clicking through all these...
That means you can operate on its forms array, just like you would do document.forms. Like this:
//put code to find the id here instead.
id='25542';
//get the document within the iframe
ifDoc=document.getElementById('myframe').contentDocument;
//access the first element of the first form (the select, you can also use the name I think)
ifDoc.forms[0].elements[0].value='20';
//change the action to use the id you specified
ifDoc.forms[0].action=ifDoc.forms[0].action+'?id='+id;
//submit the form
ifDoc.forms[0].submit();
Hopefully that makes some sense.
posted by !Jim at 5:01 PM on May 14, 2007