Monday, December 10, 2007

Javascript button click event - Firefox

In some instances we may need to perform a button click event through JavaScript.

This would be look like this:

<script language="javascript">
function doSomeThing();
{
//do something
var btn = document.getElementByID('btnHidden');
btn.click();
}
</script>


This will works fine in IE, but wont work in Firefox.
To get it work in Firefox add UseSubmitBehavior="false" property to button.

This will make the button postback with the ASP.NET postback model.
ex:-

<div style="display:none;">
<asp:Button id="btnHidden" runat="server"
OnClick="btnHidden_Click"
UseSubmitBehavior="false"
/>
</div>

How it works: Compare html generated by .Net framework with UseSubmitBehavior and without this.

With UseSubmitBehaviour="false" it will render onclick event for button & without this it wont render onclick event.

No comments: