Radio button onclick....

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

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"} %> <br/>

<%= 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)

<script> //disable text_field 2 (and enable 1) when button 1 is clicked jQuery("#button1:checked").each(function()   jQuery("#field2")[0].disabled = true;   jQuery("#field1")[0].disabled = false; });

//and vice versa jQuery("#button2:checked").each(function()   jQuery("#field1")[0].disabled = true;   jQuery("#field2")[0].disabled = false; });

</script>

this is all untested!

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

good luck.

Max Williams 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"} %> <br/>

<%= 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)

<script> //disable text_field 2 (and enable 1) when button 1 is clicked jQuery("#button1:checked").each(function()   jQuery("#field2")[0].disabled = true;   jQuery("#field1")[0].disabled = false; });

//and vice versa jQuery("#button2:checked").each(function()   jQuery("#field1")[0].disabled = true;   jQuery("#field2")[0].disabled = false; });

</script>

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:

<script>

jQuery(document).ready(function() {

  //disable text_field 2 (and enable 1) when button 1 is clicked   jQuery("#button1:checked").each(function()     jQuery("#field2")[0].disabled = true;     jQuery("#field1")[0].disabled = false;   });

  //and vice versa   jQuery("#button2:checked").each(function()     jQuery("#field1")[0].disabled = true;     jQuery("#field2")[0].disabled = false;   });

} </script>

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

<script type="text/javascript">

not just "<script>".

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

Thankx... Max Williams 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 ...........

<script language="javascript" type="text/javascript">

jQuery(document).ready(function() {

  //disable text_field 2 (and enable 1) when button 1 is clicked   jQuery("#button1:checked").each(function() {     jQuery("#field2")[0].disabled = true;     jQuery("#field1")[0].disabled = false;   })

  //and vice versa   jQuery("#button2:checked").each(function() {     jQuery("#field1")[0].disabled = true;     jQuery("#field2")[0].disabled = false;   }) });

</script>

<%# 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

Soutom Soutom wrote:

Thankx... Max Williams 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?