Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pressing enter inside a textbox does not fire onclick event of submit button in firefox

I have this html page which is very simple, it contains a text box and a submit button, what I want is when typing something in the text box and pressing enter the function in the onclick event of the button gets called. It's working in IE and Google Chrome but not working in FireFox, is this a normal behavior of FireFox or there's something am missing here?

<html>
<head>
<script language="javascript">
function callMe()
{
   alert("You entered: " + document.getElementById("txt").value);
}
</script>
</head>
<body>
<input type="text" id="txt" />
<input type="submit" value="Submit" onclick="callMe()" />
</body>
</html>
like image 676
Yasmine Avatar asked Jan 01 '26 18:01

Yasmine


2 Answers

From the description of the onclick event:

The onclick event occurs when the pointing device button is clicked over an element. This attribute may be used with most elements.

There is no guarantee there that the UA should generate such an event even when not using a pointing device and clicking something.

You probably want the onsubmit event on a form:

The onsubmit event occurs when a form is submitted. It only applies to the FORM element.

You'll need to wrap a form around your text field and button, though:

<html>
  <head>
    <script language="javascript">
      function callMe()
      {
        alert("You entered: " + document.getElementById("txt").value);
      }
    </script>
  </head>
  <body>
    <form onsubmit="callMe()" action="#">
      <input type="text" id="txt" />
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>
like image 78
Joey Avatar answered Jan 03 '26 09:01

Joey


Try adding a form with an onsubmit - this should work (tested with FF 3.5):

<html>
<head>
<script language="javascript">
function callMe()
{
   alert("You entered: " + document.getElementById("txt").value);
}
</script>
</head>
<body>
<form onsubmit="callMe()">
<input type="text" id="txt" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
like image 24
Dror Avatar answered Jan 03 '26 11:01

Dror



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!