Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
Hey guys. I have a website I need to fill out my personal info on. I have the following script to add my info into text fields:

Code:
set firstName to "Your Name"
tell application "Safari"
   do JavaScript "document.getElementsByName('fvFirst')[0].value='" & firstName & "';" in document 1
end tell

I have to select my gender by clicking "male"

I don't know the Javascript code to do so. I posted the source code from the page below if it helps anyone. Thanks so much.

Code:
<h6>Gender</h6>
<input type="radio" value="m" name="fvGender"/>
<label class="radio">Male</label>
<input type="radio" value="f" name="fvGender"/>
<label class="radio">Female</label>
</div>
</div>
 

Sijmen

macrumors 6502a
Sep 7, 2005
709
1
If you can get the form with getElementById() or getElementsByTag(), you can call submit() on it.

In case that's not what you meant, please tell :)
 

angelwatt

Moderator emeritus
Aug 16, 2005
7,852
9
USA
Give the input elements ID attributes, then you can do,
PHP:
document.getElementById('fvGenderM').checked = true;

Or, since this is only going to be run in Safari,
PHP:
document.getElementsByName('fvGender')[0].checked = true;
 

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
Give the input elements ID attributes, then you can do,
PHP:
document.getElementById('fvGenderM').checked = true;

Or, since this is only going to be run in Safari,
PHP:
document.getElementsByName('fvGender')[0].checked = true;

Your second option did it. it's written as follows for those in need:

Code:
tell application "Safari"
	do JavaScript "document.getElementsByName('fvGender')[0].checked = true;" in document 1
end tell

Thanks Again.
 

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
I want to click the submit button now, but the following code doesn't seem to be working. What am I doing wrong here?

Source page:

Code:
name="submitButton"/>

I tried the following:

Code:
tell application "Safari"
	do JavaScript "document.getElementsByName('submitButton')[0].click()" in document 1
end tell

and

Code:
tell application "Safari"
	do JavaScript "document.getElementsByName('submitButton')[0].onclick()" in document 1
end tell

and

Code:
tell application "Safari"
	do JavaScript "document.getElementsByName('submitButton')[0].submit()" in document 1
end tell

Any ideas?
 

angelwatt

Moderator emeritus
Aug 16, 2005
7,852
9
USA
Try using a name on the form where you have some action set.
HTML:
<form name="theform" action="somepage.php" method="post">
...
</form>

Then, for JavaScript,
PHP:
document.getElementsByName('theform')[0].submit();

It may also work to use,
PHP:
document.theform.submit();
 

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
got form id and used this guy and it worked. Thanks!

Code:
document.getElementsByName('theform')[0].submit();
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.