# Newbie: How to use text\_field with Java Script

**URL:** <https://www.ruby-forum.com/t/newbie-how-to-use-text-field-with-java-script/100910>\
**Category:** Rails\
**Created:** [August 1, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/newbie-how-to-use-text-field-with-java-script/100910 "2006-08-01T22:35:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [August 1, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/newbie-how-to-use-text-field-with-java-script/100910/1 "2006-08-01T22:35:36Z")

</div>

How could I generate above text filed using ruby?

I am not sure how to give the options such as onchange, onfocus, id and  
so on?

could some one please help.

---

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [August 1, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/newbie-how-to-use-text-field-with-java-script/100910/2 "2006-08-01T22:53:35Z")

</div>

You can specify that behaviour in the options parameter of the  
text\_field  
method in FormHelper. For example, you can do the following:  
\<%= text\_field :object, ‘field’, :onchange =\> “your onchange javascript  
here”, :onfocus =\> “your onfocus javascript here” %\>

HTH,  
Leendert

---

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [August 1, 2006, 11:13pm UTC](https://www.ruby-forum.com/t/newbie-how-to-use-text-field-with-java-script/100910/3 "2006-08-01T23:13:04Z")

</div>

Leendert Brouwer wrote:

> You can specify that behaviour in the options parameter of the  
> text\_field  
> method in FormHelper. For example, you can do the following:  
> \<%= text\_field :object, ‘field’, :onchange =\> “your onchange javascript  
> here”, :onfocus =\> “your onfocus javascript here” %\>
> 
> HTH,  
> Leendert

Hi I did try it and it throws an error fallowing is my code

\<%= text\_field ‘reservation’, ‘start\_time’, :id =\> “dateField”,  
:onchange =\>“magicDate(‘dateField’);”, :onfocus=“if (this.className !=  
‘error’) this.select()” %\>

I believe the error occurs in onfocuse area as It might be getting  
confused with the input

Could you someone pls help me to correct this?

---

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [August 2, 2006, 3:31am UTC](https://www.ruby-forum.com/t/newbie-how-to-use-text-field-with-java-script/100910/4 "2006-08-02T03:31:45Z")

</div>

Leendert Brouwer wrote:

> On 8/1/06, Ruwan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi I did try it and it throws an error fallowing is my code
> > 
> > \<%= text\_field ‘reservation’, ‘start\_time’, :id =\> “dateField”,  
> > :onchange =\>“magicDate(‘dateField’);”, :onfocus=“if (this.className !=  
> > ‘error’) this.select()” %\>
> > 
> > I believe the error occurs in onfocuse area as It might be getting  
> > confused with the input
> 
> What’s the error? Are you getting a rails error, or a javascript error?

I am getting a rails error on this area :onfocus="if (this.className !=

> > ‘error’) this.select()"

its pointing the error on :onfocus=" \<\<\<\< on the quote symbol.

Any ideas?

---

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [August 1, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/newbie-how-to-use-text-field-with-java-script/100910/5 "2006-08-01T23:18:11Z")

</div>

On 8/1/06, Ruwan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi I did try it and it throws an error fallowing is my code
> 
> \<%= text\_field ‘reservation’, ‘start\_time’, :id =\> “dateField”,  
> :onchange =\>“magicDate(‘dateField’);”, :onfocus=“if (this.className !=  
> ‘error’) this.select()” %\>
> 
> I believe the error occurs in onfocuse area as It might be getting  
> confused with the input

What’s the error? Are you getting a rails error, or a javascript error?

---

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [August 2, 2006, 3:39am UTC](https://www.ruby-forum.com/t/newbie-how-to-use-text-field-with-java-script/100910/6 "2006-08-02T03:39:48Z")

</div>

On 8/2/06, woow [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > >
> 
> Any ideas?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

You have a typo. Should be :onfocus =\> " whereas you have only the =

---

<div class="post-metadata">

**Author:** ![woow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@woow](https://www.ruby-forum.com/u/woow)\
**Post date:** [August 2, 2006, 3:43am UTC](https://www.ruby-forum.com/t/newbie-how-to-use-text-field-with-java-script/100910/7 "2006-08-02T03:43:19Z")

</div>

On Wednesday, August 02, 2006, at 3:31 AM, woow wrote:

> > > I believe the error occurs in onfocuse area as It might be getting  
> > > Any ideas?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Try this instead…

\<%= text\_field :object, ‘field’, {}, {:onchange =\> “your onchange  
javascript  
here”, :onfocus =\> “your onfocus javascript here”} %\>

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)
