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.
Af3cecc8af253b5acd3c09c5b67c0074?d=identicon&s=25 Valentino Lun (on9west)
on 2009-04-17 10: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
27c1d5470227584808ba5d88c022e894?d=identicon&s=25 Charanya Nagarajan (charanya)
on 2009-04-17 13: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.
44391f94143d129bdfdcd8b52aa51674?d=identicon&s=25 Schalk Neethling (Guest)
on 2009-04-17 14: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.