# Button\_to\_function & safari 3

**URL:** <https://www.ruby-forum.com/t/button-to-function-safari-3/122611>\
**Category:** Rails\
**Created:** [December 10, 2007, 4:12am UTC](https://www.ruby-forum.com/t/button-to-function-safari-3/122611 "2007-12-10T04:12:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pat](https://www.ruby-forum.com/u/pat)\
**Post date:** [December 10, 2007, 4:12am UTC](https://www.ruby-forum.com/t/button-to-function-safari-3/122611/1 "2007-12-10T04:12:13Z")

</div>

Has anyone else noticed that buttons rendered with button\_to\_function  
in Safari don’t work when you hit return? Works find in firefox. Is  
there a know work around?

---

<div class="post-metadata">

**Author:** ![pat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pat](https://www.ruby-forum.com/u/pat)\
**Post date:** [December 10, 2007, 4:32am UTC](https://www.ruby-forum.com/t/button-to-function-safari-3/122611/2 "2007-12-10T04:32:40Z")

</div>

Hm… I just did a quick test and when I tabbed to the button and  
pressed return, it submitted the form. Maybe you have something else  
amiss?

Safari 3.0.4 (523.12) on Mac OS X 10.4.11 and Windows XP SP2.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![pat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pat](https://www.ruby-forum.com/u/pat)\
**Post date:** [December 10, 2007, 4:43am UTC](https://www.ruby-forum.com/t/button-to-function-safari-3/122611/3 "2007-12-10T04:43:15Z")

</div>

On Dec 9, 2007, at 9:11 PM, Pat wrote:

> Has anyone else noticed that buttons rendered with button\_to\_function  
> in Safari don’t work when you hit return? Works find in firefox. Is  
> there a know work around?
> 
> >

Gack…

Sorry, Pat. Just looked a little closer and saw that you said  
button\_to\_function, not button\_to. That’s a different story, but I  
do have some information for you. I ran into the same problem last  
night and it’s because there’s no form element. According to someone  
on the web-dev Apple list, it’s a bug in Safari (which I have not yet  
filed…Maybe we both should 🙂 ). You can get around it in one of  
two ways:

1. put a form around the 
2. change it to …

Granted, that means you will have to create your html without using  
button\_to\_function, but it will work.

Here’s the text an the exchange I had last night/this morning:

===============================================

Thank you Alexey and Tom. I tried two things:

1. changing \<input type=“button” …\> to 

and

1. putting a around the input.

Both achieved the desired result, but I have to ask which way is more  
correct? My specific scenario is I have a login form with two  
buttons, one for “Login” and one for “Forgot Password”, each calling  
a JavaScript function. In the case of Forgot Password, it’s a simple  
redirect with

window.location = url;

But for the login form, I submit the username and password via  
Prototype and AJAX, so when the user enters incorrect login  
credentials, I update the specific portion of the screen by rendering  
a partial (this is a Rails app). I chose to do this so I wouldn’t  
have to redraw the whole page again because it has flash animations  
and graphics (trying to keep it snappy).

In addition to the previous two examples, I suppose there is a third  
possibility:

1. Use two forms, one for the login piece and one for the Forgot  
Password button

Which way would be considered the closest to correct?

Peace,  
Phillip

On Dec 9, 2007, at 6:02 AM, TÃ³mas Ãrnason wrote:  
Try using a button-tag instead, might work. As you don’t have a form-  
element surrounding the input-button.

Also, check out the JavaScript console in Safari and check the output…

Tom

On Dec 9, 2007, at 2:33 AM, Alexey Proskuryakov wrote:  
on 09.12.2007 07:37, Phillip K. at removed\_email\_address@domain.invalid wrote:

When the button is highlighted, IE 6 and FireFox 2.0.0.11 (Windows  
and Mac) both fire the onclick event when the Enter/Return key is  
pressed. Safari 3 (neither Windows or Mac) doesn’t.

Safari simulates onclick on form’s submit element, not the button  
(and if  
there is no submit, it doesn’t simulate a click). Please file a bug via  
[http://bugs.webkit.org](http://bugs.webkit.org)!

doesn’t have this problem AFAICT.

Is there some special way of getting that to work in Safari without  
resorting  
to trapping the key in an onkeypress event?

Besides using , I would suggest onkeydown over onkeypress,  
although in this specific case it doesn’t make a difference.

- WBR, Alexey Proskuryakov.

Original Message:

Hi all. First post to this list. I apologize if this is not a unique  
question. I’ve been searching for the past hour, but have not found  
an answer so I thought I’d try here.

I have a simple page

When the button is highlighted, IE 6 and FireFox 2.0.0.11 (Windows  
and Mac) both fire the onclick event when the Enter/Return key is  
pressed. Safari 3 (neither Windows or Mac) doesn’t. Is there some  
special way of getting that to work in Safari without resorting to  
trapping the key in an onkeypress event?

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![pat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pat](https://www.ruby-forum.com/u/pat)\
**Post date:** [December 10, 2007, 5:18am UTC](https://www.ruby-forum.com/t/button-to-function-safari-3/122611/4 "2007-12-10T05:18:53Z")

</div>

In addition to my second message, I also found this link on the  
WebKit Bugzilla:

[http://bugs.webkit.org/show\_bug.cgi?id=14382](http://bugs.webkit.org/show_bug.cgi?id=14382)

It’s not the same test case, but the bug is the same. However, part  
way down the comments, a person named Dave Hyatt states

========== begin comment ===========  
There is no bug on Mac. This is really a Windows port issue.

On the Mac, spacebar activates a button, and Enter activates the  
default button  
in dialogs. The two are distinct. This is expected behavior to a Mac  
user.

On Windows this is not the case (Enter always activates the focused  
button, and  
only activates the default button if another button is not focused).  
========== end comment ===========

Since I’m new to the Mac (past 7 months or so), this was new to me.  
I did test my case again using the space bar and it worked fine.

Go figure.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![pat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pat](https://www.ruby-forum.com/u/pat)\
**Post date:** [December 10, 2007, 4:50am UTC](https://www.ruby-forum.com/t/button-to-function-safari-3/122611/5 "2007-12-10T04:50:15Z")

</div>

hmmm, well here is my exact code. See anything?

 \<%= button\_to\_function "Add an Element", remote\_function(:url =\> {:action =\> 'insert\_element\_controls'}) %\>
