views:

183

answers:

2

Are there any suggestions out there for setting a default enter button that can work not just in IE but in Firefox, Chrome, Safari and so on. I've got a number of panels with the default button set but when I hit enter in a non ie browser it just defaults to the first link on the page.

Any suggestions?

+1  A: 

You could write a bit of javascript that checks which button it is that is pressed - if it was the enter key, carry out your desired action.

Check for enter with something like:

function checkEnter()
{
if(event)
{
if(evnet.keyCode == 13) //ur code
}
else if(evt)
{
if(evt.keyCode == 13) //ur code
}
}
Paul
How do you suggest detecting where it was called from? It may very well be buried in some child control somewhere.
Middletone
bit awkward, but... set a global js variable. Whenever a control gains focus, set this variable to the controlID.
Paul
+1  A: 

I've provided 2 examples here, the first one using jQuery while the second uses old school javascript.

jQuery

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt; 
<script language="javascript" type="text/javascript">
    $("document").ready(function(){
        $(".searchBox").keypress(function(e){
         if (e.keyCode == 13)
          //add code here

        });
    });
</script>

<div class="searchBox">
    <input type="text" name="searchText"/>
    <input type="submit" value="search now" />
</div>

Pure javascript

I've done this before using the "onkeypress" action on a div that surrounds the form. This was tested in IE7, Firefox3, and Chrome.

here's an example:

<div id="searchBox" onkeypress="return clickButton(event,'btnSearch')">
    <input name="searchText" type="text" id="searchText"  />
    <input type="button" name="btnSearch" value="Search" id="btnSearch" onClick="SubmitAction()" /> 
</div>


<script type="text/javascript" language="javascript"> 
    function SubmitAction()
    {
        alert('button clicked');
    }

    function clickButton(e, btId){ 
          var bt = document.getElementById(btId);      
          if (typeof bt == 'object'){ 
                if(navigator.appName.indexOf("Netscape")>(-1)){ 
                      if (e.keyCode == 13){ 
                            bt.click(); 
                            return false; 
                      } 
                } 
                if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){ 
                      if (event.keyCode == 13){ 

                            bt.click();
                            event.cancelBubble = true; 
                            return false; 
                      } 
                } 
          } 
    }
</script>

You may have to edit this to make it fully compatible across all browsers but you should get the idea from it. Essentially any keypress while this div is focussed will call the clickButton() function. As mentioned by other answers the e.keyCode of 13 represents the Enter keypress.

NickGPS
How would this be affected if the links were asp.net controls as in a webforms app?
Middletone
This code is designed to capture all enter-key presses that occur when the focus is set inside a <div> on your page. You would use jQuery or native javascript to action the button/link you want as the default.In jQuery I think you'd put $('#yourASPNETGeneratedButtonId').click();. I don't use jQuery much though so I'd suggest looking it up, rather than take my word for it.
NickGPS