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>
From the description of the onclick event:
The
onclickevent 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
onsubmitevent occurs when a form is submitted. It only applies to theFORMelement.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With