# Radio button onclick

**URL:** <https://www.ruby-forum.com/t/radio-button-onclick/196714>\
**Category:** Rails\
**Created:** [June 13, 2008, 2:58pm UTC](https://www.ruby-forum.com/t/radio-button-onclick/196714 "2008-06-13T14:58:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![soutom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@soutom](https://www.ruby-forum.com/u/soutom)\
**Post date:** [June 13, 2008, 2:58pm UTC](https://www.ruby-forum.com/t/radio-button-onclick/196714/1 "2008-06-13T14:58:46Z")

</div>

i am trying to do some thing with radio button.

my main tergate is that i have 2 radio button and 2 text field.  
when i click on a radio button1, then the corrosponding text field say  
text1 will b active, and another text field say text2 will b inactive.

and again when if we click on the other radio button2 then the  
corrosponding text field say text2 will be active and text1 will b  
inactive…

NOTE : Basically i am very new in Ruby On Rails…so i cant do this.  
if anyone kindly hepl me i will be very much thankful to him/her

---

<div class="post-metadata">

**Author:** ![soutom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@soutom](https://www.ruby-forum.com/u/soutom)\
**Post date:** [June 13, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/radio-button-onclick/196714/2 "2008-06-13T15:49:41Z")

</div>

Soutom Soutom wrote:

> i am trying to do some thing with radio button.
> 
> my main tergate is that i have 2 radio button and 2 text field.  
> when i click on a radio button1, then the corrosponding text field say  
> text1 will b active, and another text field say text2 will b inactive.
> 
> and again when if we click on the other radio button2 then the  
> corrosponding text field say text2 will be active and text1 will b  
> inactive…
> 
> NOTE : Basically i am very new in Ruby On Rails..so i cant do this.  
> if anyone kindly hepl me i will be very much thankful to him/her

have a look at jQuery, which is perfect for this sort of thing. You’ll  
need to know a little bit of javascript (syntax mainly) but that’s  
pretty much essential for web dev.

You could set up the html as follows (this is inside a form):

\<%# checked by default %\>  
\<%= radio\_button\_tag “field”, 1, true, {:id =\> “button1”} %\>  
\<%= text\_field\_tag :field1, “”, {:id =\> “field1”} %\>

\<%= radio\_button\_tag “field”, 2, false, {:id =\> “button2”} %\>  
\<%# disabled by default %\>  
\<%= text\_field\_tag :field1, “”, {:id =\> “field2”, :disabled =\> true} %\>

then, at the top of the page, you could have the following script  
(obviously it will only work once you’ve installed jQuery)

this is all untested!

jQuery is fantastic and has loads of really useful plugins - it’s really  
essential.

good luck.

---

<div class="post-metadata">

**Author:** ![soutom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@soutom](https://www.ruby-forum.com/u/soutom)\
**Post date:** [June 13, 2008, 3:53pm UTC](https://www.ruby-forum.com/t/radio-button-onclick/196714/3 "2008-06-13T15:53:31Z")

</div>

Max W. wrote:

> Soutom Soutom wrote:
> 
> > i am trying to do some thing with radio button.
> > 
> > my main tergate is that i have 2 radio button and 2 text field.  
> > when i click on a radio button1, then the corrosponding text field say  
> > text1 will b active, and another text field say text2 will b inactive.
> > 
> > and again when if we click on the other radio button2 then the  
> > corrosponding text field say text2 will be active and text1 will b  
> > inactive…
> > 
> > NOTE : Basically i am very new in Ruby On Rails..so i cant do this.  
> > if anyone kindly hepl me i will be very much thankful to him/her
> 
> have a look at jQuery, which is perfect for this sort of thing. You’ll  
> need to know a little bit of javascript (syntax mainly) but that’s  
> pretty much essential for web dev.
> 
> You could set up the html as follows (this is inside a form):
> 
> \<%# checked by default %\>  
> \<%= radio\_button\_tag “field”, 1, true, {:id =\> “button1”} %\>  
> \<%= text\_field\_tag :field1, “”, {:id =\> “field1”} %\>
> 
> \<%= radio\_button\_tag “field”, 2, false, {:id =\> “button2”} %\>  
> \<%# disabled by default %\>  
> \<%= text\_field\_tag :field1, “”, {:id =\> “field2”, :disabled =\> true} %\>
> 
> then, at the top of the page, you could have the following script  
> (obviously it will only work once you’ve installed jQuery)
> 
> this is all untested!
> 
> jQuery is fantastic and has loads of really useful plugins - it’s really  
> essential.
> 
> good luck.

Oops., i forgot one important thing! The jQuery commands need to go  
inside a ‘document ready’ block, which is run as soon as the page has  
loaded. So, the script element should be:

---

<div class="post-metadata">

**Author:** ![soutom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@soutom](https://www.ruby-forum.com/u/soutom)\
**Post date:** [June 13, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/radio-button-onclick/196714/4 "2008-06-13T15:59:09Z")

</div>

oops again! and, of course, the script start tag should be

not just "". This must be a friday...i never could get the hang of fridays.

---

<div class="post-metadata">

**Author:** ![soutom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@soutom](https://www.ruby-forum.com/u/soutom)\
**Post date:** [June 14, 2008, 11:40am UTC](https://www.ruby-forum.com/t/radio-button-onclick/196714/5 "2008-06-14T11:40:09Z")

</div>

Thankx… Max W. for your helphul informatin…

still i am faceing some problem…the text field2 is not working with  
the corrosponding radio button.  
Even when i select the radio button2 then also text field2 also  
disable & textfield 2 also enable…

my total code with javascript is …

\<%# checked by default %\>  
\<%= radio\_button\_tag “field”, 1, true, {:id =\> “button1”} %\>  
\<%= text\_field\_tag :field1, “”, {:id =\> “field1”} %\>

\<%= radio\_button\_tag “field”, 2, false, {:id =\> “button2”} %\>  
\<%# disabled by default %\>  
\<%= text\_field\_tag :field2, “”, {:id =\> “field2”, :disabled =\> true}  
%\>

will tou plese hepl me to solv this problem…  
thanking you

---

<div class="post-metadata">

**Author:** ![soutom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@soutom](https://www.ruby-forum.com/u/soutom)\
**Post date:** [June 16, 2008, 10:33am UTC](https://www.ruby-forum.com/t/radio-button-onclick/196714/6 "2008-06-16T10:33:21Z")

</div>

Soutom Soutom wrote:

> Thankx… Max W. for your helphul informatin…
> 
> still i am faceing some problem..the text field2 is not working with  
> the corrosponding radio button.  
> Even when i select the radio button2 then also text field2 also  
> disable & textfield 2 also enable..

Would you mind opening the page source and copying the section with the  
fields and radio buttons, and posting it up here?
