Hit Enter as well as Button click

Hi,

I am having trouble with something, my client wants to be able to hit <enter> as well as click the button on this page:-

http://kidsunlimi2.eweb102.discountasp.net/find-nursery.aspx

If you look at the Blue “Postcode search” and try searching for a nursery byb clicking the button you will see how it works. However then try doing the same but hitting <enter> and see what happens…

The front end html is as follows:-


    <script type="text/javascript" language="javascript">
           TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + butt.UniqueID + "').click();return false;}} else {return true}; ");  
    </script>
    
    <div id="postcode-right">
    <ul>
    <li>Enter your Postcode or Town/City</li>
    <li><input type="text" ID="TextBox1" style="width:220px; border:1px solid black;" /></li>
    <li><input type="button" ID="butt" class="postcode-submit" onclick="return butt_onclick()"/></li>
    </ul>
    </div>

I tried adding javascript so it would recognize the button and allow users to hit enter but this did not work.

Why do i get this problem? And how can i solve it?

Thanks in advance.

If you notice, the search actually works when you hit enter, but it stops and then pushes up the page for reason…

??

:frowning:



<SCRIPT TYPE="text/javascript">
<!--
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}
//-->
</SCRIPT>

On the textbox:

onKeyPress=“return submitenter(this,event)” Note: I reccomend you add the code in an external file. That goes for the code above also.

Also, for this to work, add a <form> tag around the textbox/button. Since it is using the form.submit().

Hey,

Thanks USPatriot. I have tried this but it doesnt seem to work…

When i hit <enter> it works for a sec but then the search vanishes and brings up a list of nurseries…

TextBox1.attributes.add()…should be VB/C# code, not javascript.

In code-behind:


TextBox1.Attributes.Add("onKeyDown", "doClick('" + TextBox1.ClientID + "',event)");



In Javascript:


<SCRIPT type=text/javascript>
    function doClick(buttonName,e)
    {


         if(window.event)
              key = window.event.keyCode;     
         else
              key = e.which;    
    
        if (key == 13)
        {
          
            var btn = document.getElementById(buttonName);
            if (btn != null)
            { 
                btn.click();
                event.keyCode = 0
            }
        }
   }
    </SCRIPT>



I made an edit.

Wrap individual forms in panels and set the DefaultButton property on the panel. That should solve your problem.

Yeah, that is another way. Like I said on another thread, 1000000 ways to do the same project. Good alternate however.

Yep! I blogged about it here: http://imaginekitty.com/255/firing-a-click-event-using-the-enter-key/