# Position Mouse Curser - RoR way

**URL:** <https://www.ruby-forum.com/t/position-mouse-curser-ror-way/82729>\
**Category:** Rails\
**Created:** [January 20, 2007, 2:01am UTC](https://www.ruby-forum.com/t/position-mouse-curser-ror-way/82729 "2007-01-20T02:01:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 20, 2007, 2:01am UTC](https://www.ruby-forum.com/t/position-mouse-curser-ror-way/82729/1 "2007-01-20T02:01:33Z")

</div>

Does anyone know how to set the mouse curser in a text box/area. For  
example:

When a user is presented with a form - I want the mouse curser  
positioned in the first form item.

Another example - I want to create a wiki-style text area where users  
can add formatting by clicking a button (think TRAC) When a user clicks  
the bold button, for example, the bold markup will display in the text  
area with the mouse positioned between the mark-up.

Know how to do this? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 20, 2007, 2:33am UTC](https://www.ruby-forum.com/t/position-mouse-curser-ror-way/82729/2 "2007-01-20T02:33:16Z")

</div>

Kim wrote:

> Does anyone know how to set the mouse curser in a text box/area. For  
> example:
> 
> When a user is presented with a form - I want the mouse curser  
> positioned in the first form item.
> 
> Another example - I want to create a wiki-style text area where users  
> can add formatting by clicking a button (think TRAC) When a user clicks  
> the bold button, for example, the bold markup will display in the text  
> area with the mouse positioned between the mark-up.
> 
> Know how to do this? Thanks in advance.

Rails has no built in way of doing this (that I know of), but it is  
easily achieved with simple javasripts.

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 20, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/position-mouse-curser-ror-way/82729/3 "2007-01-20T14:36:33Z")

</div>

The RoR way of doing this is not to do it. Users would hate it, and  
it’s very not-RoR.

On Jan 20, 2:33 am, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 20, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/position-mouse-curser-ror-way/82729/4 "2007-01-20T18:18:56Z")

</div>

why would you need the pointer positioned over a form field? i can’t  
see how that would serve any kind of purpose.

now, if you really mean you want the first form element to have focus,  
that’s quite different and is used quite often.

if using the prototype library, just give your form input elements an  
id (if you are using rails helper methods to generate input fields,  
this will already be done for you)

make sure to put this somewhere after the form itself.

for extra credit, add a class name to your fields and when the field  
is focused it will stand out more.

in your css add something like

.field:focus {  
background-color: #ffffcc;  
}

i believe that will work across all browsers ( i know safari and  
firefox handle it ok ).

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 22, 2007, 3:53am UTC](https://www.ruby-forum.com/t/position-mouse-curser-ror-way/82729/5 "2007-01-22T03:53:45Z")

</div>

Yes, what I want is to have the curser pre-positioned in the first  
field - focus will do it.

Thanks

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 22, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/position-mouse-curser-ror-way/82729/6 "2007-01-22T19:14:13Z")

</div>

> > make sure to put this somewhere after the form itself.

I can not get this to work - can you provide an example? Can’t I put  
this in the JS files so it does not have to be on every form, and then  
call the field id in each form that I want to have the focus a common  
name?

Thanks, K

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 22, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/position-mouse-curser-ror-way/82729/7 "2007-01-22T19:50:20Z")

</div>

example:

 \<%= javascript\_include\_tag "prototype %\> 

On 1/22/07, Kim [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> call the field id in each form that I want to have the focus a common  
> name?

there’s nothing stopping you from doing that, no. but i fail to see  
the reason why. you still have to have a line of javascript in your  
template in order to actually focus the element. if you put it in a  
separate file, that file has to be loaded off the server, vs. having  
it inline. also (not sure about this), i’d be concerned about how the  
browser loads external files and to when they are interpreted. ie, is  
it going to load/interpret that js file prior to rendering the form?

focus.js  
---- snip -----  
Field.focus(‘element\_id’);  
---- snip -----

template  
---- snip -----

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 22, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/position-mouse-curser-ror-way/82729/8 "2007-01-22T20:21:28Z")

</div>

Thanks for the example - but not applicable - I am not using prototype.  
Here is what I have tried - still not working.

start\_form\_tag :action =\> ‘register’, :id =\> @user  
Email  
text\_field ‘user’, ‘email’  
submit\_tag ‘Register’  
end\_form\_tag

```
<script type="text/javascript">
    Field.focus('user_email');
</script>
```

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 22, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/position-mouse-curser-ror-way/82729/9 "2007-01-22T20:41:32Z")

</div>

well your choices are:

1. use prototype javascript library (it’s installed as a part of every  
new rails project)
2. use the base javascript functionality to apply focus to a form  
element. there are a myriad of samples/tutorials on the web at your  
disposal.
