Peter said:
I have the following code in a form:
<input type="button" value="Search for this City"
onClick="CreateDate()">
If I click the button is runs the javascript correctly but if I press
enter it doesn't run correctly. Is there something I can put in the
form so if the user presses return or clicks the button that it runs
the javascript CreateDate() ?
Despite its name, the `click' event for an element also occurs when a
confirmation key (here: Enter) is pressed while the element has the
focus in the following user agents:
- Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11)
Gecko/20071127 Firefox/2.0.0.11
- Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;
{3E1C4754-F096-BBFE-CD76-3B2E8F19E202}; .NET CLR 2.0.50727)
- Opera/9.24 (Windows NT 5.1; U; en)
The Location Bar test code was:
javascript:document.open("text/html"); document.write('<input type="button"
value="foo" onclick="window.alert(42);">'); document.close();
So the reasons why it does not work for you include:
A) You have been testing with Safari (here: 3.0.4 Beta):
Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE) AppleWebKit/523.15
(KHTML, like Gecko) Version/3.0 Safari/523.15
In that user agent, the `click' event occurs only when then button
is clicked or when you press the space bar while it has the focus.
Although that the pointing device is required is in fact compliant with
HTML 4.01, section 18.2.3, and W3C DOM Level 2 Events, section 1.6.2,
I would consider the entire behavior (Space works, Enter doesn't)
a bug that should be reported.
B) Invalid markup, see <
http://validator.w3.org/>.
C) A deliberate condition or an error in the CreateDate() method
(you have not posted the code of that method).
D) You have been testing with a not-so-common user agent that I could
therefore not test with (you have not said which one that has been).
E) You have been testing with a user agent that is influenced by
another application (e.g. a software firewall and/or a popup blocker).
Try disabling or uninstalling that application, then test again.
See also
http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1Quest
PointedEars