Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xp sp2 and clicking a button through js

Status
Not open for further replies.

smsinger3

Programmer
Oct 5, 2000
192
US
Hello, I'm having a big problem trying to get a javascript function to click a button to submit a page in IE. I *think* it may be related to Windows XP SP2, but I'm not sure. Here's what doesn't work. If I try to click the link, the page submits to the root of the website. Not to the page specified in the <form> tag. Here's an example:

Code:
<input type="submit" name="btnSubmitMe" value="Click Me" id="btnSubmitMe" />
<br>
<a href="" onclick="javascript:ClickButton()">Click me to submit the page through clicking the button</a>
<script language="javascript" type="text/javascript">
   function ClickButton() {
      document.Form1.btnSubmitMe.click()
	}	
</script>

However, I can get this to work if I don't call the ClickButton() function through an anchor tag. For instance, this always works:

Code:
<input type="submit" name="btnSubmitMe" value="Click Me" id="btnSubmitMe" />
<br>
<script language="javascript" type="text/javascript">

   ClickButton()

   function ClickButton() {
      document.Form1.btnSubmitMe.click()
	}	
</script>

Can anyone tell me how to fix the first set of code?

Thank you VERY MUCH for your help!

Steve
 
Don't try and emulate a click on the submit button - just call the form's submit method instead. Change this:

Code:
document.Form1.btnSubmitMe.click()

to this:

Code:
document.Form1.submit();

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Dan, thanks for the quick response. That doesn't work either. I get the same thing as before. Any other ideas?
 
Hi,
What is the action= set to in the form?



[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
The form tag looks like this:

<form name="Form1" method="post" action="WebForm1.aspx" id="Form1">

** Note: the page should post to a Asp.Net page **
 
I finally figured out the problem!!! The href attribute MUST have something in there, even if it's dummy data.

This:

Code:
<a href="" onclick="javascript:ClickButton()">Click me to submit the page through clicking the button</a>

should be:

Code:
<a href="#DUMMY" onclick="javascript:ClickButton()">Click me to submit the page through clicking the button</a>

Thanks to all who helped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top