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