Search code examples
javascriptclicklinkbuttonasplinkbutton

Why do I get an error with .Click() Event for an Asp:LinkButton in Firefox?


I am attempting to fire the click() event when a button is clicked.

This currently works for me in IE however I receive the following error in firefox:

"link.click is not a function"

I have been trawling google regarding this and found out the .click event is not supported in all versions of firefox.

Is anyone able to provide an alternative? Code below:

<asp:LinkButton ID="ButtonNext" runat="server" CssClass="RemoveLinkStyle" TabIndex="1"></asp:LinkButton>
<table id="tblButtonNext" runat="server" cellpadding="0" cellspacing="0" class="pwbtn" onclick="ButtonClick(ButtonNext);" TabIndex="1" onmouseout="this.className='pwbtn';" onmouseover="this.className='pwbtnh';">
<tr>
    <td class="a1"></td>
    <td class="a2">
        <%= this.resourceManager.GetString("nextstep") %>
    </td>
    <td class="a3"></td>
    <td class="spacer"></td>
</tr>
</table>

function ExecuteLink(linkID)
{
    var link = document.getElementById(linkID);
    link.click();
}

function ButtonClick(linkID)
{
    PreNavigationScript();
    CallShowBlocker();
    ExecuteLink(linkID); 
}

Solution

  • I'd use jQuery for this. The click event exposed by jQuery is designed to be cross browser compatible and you'll likely find it works more consistently than what you're currently using.

    Your method will then become:

     function ExecuteLink(linkID)
    {
       $("#" + linkID).click();
    }
    

    (You probably want to check whether $(linkID) returns an element also)