# Radio Button events

**URL:** <https://www.ruby-forum.com/t/radio-button-events/102114>\
**Category:** Rails\
**Created:** [August 10, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/radio-button-events/102114 "2006-08-10T23:52:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 10, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/radio-button-events/102114/1 "2006-08-10T23:52:26Z")

</div>

Hi,

I have observers for a group of radio buttons, an observe\_field for each  
radio button (I think its not possible to have one observe\_field for a  
group of radio buttons).

Now, the problem is that when I click on one radio button, all the  
observers get trigerred, so the method which the observe\_field calls  
gets the wrong result. I just want that method to get the radio button  
that is clicked, which seems quite obvious, no?

Any idea how to do this? Help would be greatly appreciated!

Thanks,

Aditya

---

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 11, 2006, 12:25am UTC](https://www.ruby-forum.com/t/radio-button-events/102114/2 "2006-08-11T00:25:17Z")

</div>

Aditya R. wrote:

> Hi,
> 
> I have observers for a group of radio buttons, an observe\_field for each  
> radio button (I think its not possible to have one observe\_field for a  
> group of radio buttons).
> 
> Now, the problem is that when I click on one radio button, all the  
> observers get trigerred, so the method which the observe\_field calls  
> gets the wrong result. I just want that method to get the radio button  
> that is clicked, which seems quite obvious, no?
> 
> Any idea how to do this? Help would be greatly appreciated!
> 
> Thanks,
> 
> Aditya

I believe radio buttons all have same id/name. You can test the value  
of the button group by:

$(‘common-radio-button-id’).value == ‘foo’

That should at least tell you whats selected.

---

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 11, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/radio-button-events/102114/3 "2006-08-11T21:55:18Z")

</div>

Thanks for the reply. Unfortunately the radio buttons have different  
id’s (though the same name). The ‘value’ is appended to the id, which  
makes the id’s different for all the buttons. Which is why I need to  
have a different observer for each button (since the observer takes the  
id name).

Moreover, $(‘common-radio-button-id’).value doesn’t work. I get a parse  
error, so probably that syntax is incorrect. Also, I don’t know if the  
radio\_button has a “value” method. I don’t see anything about it in the  
Rails API.

Any other ideas?

> I believe radio buttons all have same id/name. You can test the value  
> of the button group by:
> 
> $(‘common-radio-button-id’).value == ‘foo’
> 
> That should at least tell you whats selected.

---

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/radio-button-events/102114/4 "2006-08-15T14:18:37Z")

</div>

Hi Aditya,

Aditya R. wrote:

(1) \> (I think its not possible to have one

> observe\_field for a group of radio buttons).

(2) \> Now, the problem is that when I click on one radio

> button, all the observers get trigerred,

Given the observed behavior in (2), what makes you believe (1) ?

Bill

---

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 15, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/radio-button-events/102114/5 "2006-08-15T22:35:07Z")

</div>

> (1) \> (I think its not possible to have one
> 
> > observe\_field for a group of radio buttons).
> 
> (2) \> Now, the problem is that when I click on one radio
> 
> > button, all the observers get trigerred,
> 
> Given the observed behavior in (2), what makes you believe (1) ?

Well, I don’t conclude (1) from (2). I believe (1) simply because as I  
said in the reply above, the ‘value’ is appended to the id, which makes  
the id’s different for all the buttons. Which means that a single  
observe\_field won’t work for a group of button.

Actually I think I made a little mistake by saying that “when I click on  
one radio button, all the observers get trigerred”. In fact, as one  
would expect, the observers that get triggered are the ones for the  
radio button that gets deselcted and the one that gets selected.

---

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [February 8, 2007, 3:43am UTC](https://www.ruby-forum.com/t/radio-button-events/102114/6 "2007-02-08T03:43:38Z")

</div>

Hi Aditya,

Can you tell me how to write observers with radio buttons. I am new to  
rails.  
Please send me code snippets if possible.

Thanks,  
Nitin.  
Aditya R. wrote:

> Hi,
> 
> I have observers for a group of radio buttons, an observe\_field for each  
> radio button (I think its not possible to have one observe\_field for a  
> group of radio buttons).
> 
> Now, the problem is that when I click on one radio button, all the  
> observers get trigerred, so the method which the observe\_field calls  
> gets the wrong result. I just want that method to get the radio button  
> that is clicked, which seems quite obvious, no?
> 
> Any idea how to do this? Help would be greatly appreciated!
> 
> Thanks,
> 
> Aditya

---

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [February 8, 2007, 4:24am UTC](https://www.ruby-forum.com/t/radio-button-events/102114/7 "2007-02-08T04:24:18Z")

</div>

Hi Nitin,

Please post the code you’re having trouble with. Or Google ‘rails  
observe\_field radio\_button’.

Best regards,  
Bill
