Position Mouse Curser - RoR way

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.

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

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)

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

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 ).

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

Thanks

> <script type="text/javascript"> > Field.focus('id_of_element_you_want_focused_at_page_load'); > </script> > > 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

example:

<html> <head>     <%= javascript_include_tag "prototype %> </head> <body> <form method="post" action="...">   <input type="text" name="field1" id="field1"/>   <input type="submit" name="Save"/> </form> <script type="text/javascript"> Field.focus('field1'); </script> </body> </html>

> > <script type="text/javascript"> > > Field.focus('id_of_element_you_want_focused_at_page_load'); > > </script> > > > > 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?

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 ----- <script src="/javascripts/focus.js"/> ---- snip -----

vs.

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

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    <label for="user_email">Email</label>         text_field 'user', 'email'     submit_tag 'Register' end_form_tag

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

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.