Forum: Ruby on Rails how to call javascript function in text_field_tag

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Valentino L. (Guest)
on 2009-04-17 12:30
Dear all

Sorry for dummy question. How can I convert a text input in form to
upper case letter in view?

Can I call the javascript function toUpperCase() in text_field_tag??
<%= text_field_tag(:sometext, nil, :size => 14) %>

I know I can achieve this using params[:sometext].upcase in controller,
but I want to do this in view. Any ideas?

Many thanks
Valentino
Charanya N. (Guest)
on 2009-04-17 15:59
<script type="text/javascript">
function changecase()
{
 (document.getElementById("sometext")).value=document.getElementById("sometext").value).toUpperCase();
}


</script>

<%= text_field_tag(:sometext, nil, :size => 14 , :onchange =>
"changecase();") %>

When u enter the text in the textbox and when the focus goes out of the
textbox the text changes to uppercase.

Hope this might help.
Schalk N. (Guest)
on 2009-04-17 16:13
(Received via mailing list)
Hi Charanya,

Or try using unobtrusive JavaScript by giving the field a unique id and
then hooking up the action to that id. With jQuery it is very simple.

To get the field just use:

$('#uniqueid')

and then you can chain any function to the element.

HTH,
Schalk
This topic is locked and can not be replied to.