button in form

P

Peter

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() ?

Thanks,
Pete
 
P

Peter

I've been searching the NET for how I need to give the OnClick focus
but everything refers to javascript and the forms outside js. Its
weird I'm not sure what its executing when I press Enter. It opens a
new window and it runs part of the commands in my js then gives an
error because it needs more info which is executed when the button is
clicked on. If it was me I'd just click the button but I can't
control the end user pressing the Enter key then complaining.

Pete
 
T

Thomas 'PointedEars' Lahn

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
 
S

Steve Swift

Randy said:
If the button has focus, IE fires the onclick if I press the Enter key.

I had the opposite problem. When I clicked in an input field in my form,
most browsers set the first button in the form to be the default action,
and it gets submitted along with the rest of the form when you press Enter.
Since the first button in my form is a "Delete" button I didn't want it
getting submitted by default.

The answer in this case was to add an invisible button as the first one
in my form, and have my CGI script ignore it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top