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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

call function when enter button is clicked

Status
Not open for further replies.

wrexhamafc234

Programmer
Oct 23, 2006
18
GB
I have the following javascript function in a seperate javascript file (javascript.js). Javascript.js is attacked in the masterpage:

Code:
function IPressedEnterBasic()
{
 if(event.which || event.keyCode)
 {
  if ((event.which == 13) || (event.keyCode == 13)) 
  {
   var save= confirm("Do you want to save?");
   if (save == true)
    {
        document.getElementById("<%=SaveButton.ClientID%>").click();
    }
    else
    {
        return false; 
    }
  }
 } 
 else 
 {
  return true;
 }
}

This function is used when the return button is clicked on a textbox, I have added the following code to the page_load event on a aspx page:

Code:
Surname.Attributes.Add("onkeydown", "return IPressedEnterBasic();");
Forename.Attributes.Add("onkeydown", "return IPressedEnterBasic();");

However, when return is clicked on either of the above textboxes, the following button click is not executed.
Code:
protected void SaveButton_Click(object sender, EventArgs e)
{
}


Any ideas where i could be going wrong?
 
AFAIK, you cannot reliably call "click" and hope for the onclick method to be called. You might be better off calling "onclick" instead, but I think you'd be better off calling the code that the onclick function calls.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top