# Observe\_field, :function, same value

**URL:** <https://www.ruby-forum.com/t/observe-field-function-same-value/170383>\
**Category:** Rails\
**Created:** [June 16, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/observe-field-function-same-value/170383 "2009-06-16T20:43:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![spencer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@spencer](https://www.ruby-forum.com/u/spencer)\
**Post date:** [June 16, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/observe-field-function-same-value/170383/1 "2009-06-16T20:43:35Z")

</div>

Hey all I’m having problems getting the correct value of a checkbox. I  
have an observe field that calls a javascript function whenever the  
checkbox is changed. But for whatever reason the checkbox value always  
seems to be 1. Perhaps I’m using the wrong syntax to get the value?..  
My code is below, any help would be greatly appreciated.

\<% remote\_form\_for :nAd, @nAd, :url =\> { :action =\> “update” } do |f| %\>  
\<%= f.check\_box :excludeAd %\>  
\<%= f.text\_area :whyExcludeAd, :cols =\> 30, :rows =\> 5 %\>  
\<% end %\>

\<%= observe\_field(“nAd\_excludeAd”, :function =\>  
“displayElementByValue(‘nAd\_excludeAd’, ‘nAd\_whyExcludeAd’);”) %\>

function displayElementByValue(elementToCheck, elementToChange)  
{  
// value is always 1!!  
var value = $(elementToCheck).value;  
formElement = document.getElementById(elementToChange);  
}

Basically when this function is called it changes the display property  
of the “nAd\_whyExcludAd” element, however I can’t get it to correctly  
show the value from “nAd\_excludeAd”, though the function is being called  
correctly.

---

<div class="post-metadata">

**Author:** ![spencer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@spencer](https://www.ruby-forum.com/u/spencer)\
**Post date:** [June 16, 2009, 8:49pm UTC](https://www.ruby-forum.com/t/observe-field-function-same-value/170383/2 "2009-06-16T20:49:26Z")

</div>

On Jun 16, 7:43 pm, Spencer R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hey all I’m having problems getting the correct value of a checkbox. I  
> have an observe field that calls a javascript function whenever the  
> checkbox is changed. But for whatever reason the checkbox value always  
> seems to be 1. Perhaps I’m using the wrong syntax to get the value?..  
> My code is below, any help would be greatly appreciated.

The value of a checkbox doesn’t change. You need to be checking  
whether its checked property is true (or use something like prototypes  
$F() method which abstracts away that sort of concern.

Fred

---

<div class="post-metadata">

**Author:** ![spencer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@spencer](https://www.ruby-forum.com/u/spencer)\
**Post date:** [June 16, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/observe-field-function-same-value/170383/3 "2009-06-16T21:01:19Z")

</div>

Oy, that’s an awful mistake! lol It’s been a while since I’ve used  
checkboxes. That did the trick, thanks for the help.

Frederick C. wrote:

> On Jun 16, 7:43ï¿½pm, Spencer R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Hey all I’m having problems getting the correct value of a checkbox. ï¿½I  
> > have an observe field that calls a javascript function whenever the  
> > checkbox is changed. ï¿½But for whatever reason the checkbox value always  
> > seems to be 1. ï¿½Perhaps I’m using the wrong syntax to get the value?..  
> > My code is below, any help would be greatly appreciated.
> 
> The value of a checkbox doesn’t change. You need to be checking  
> whether its checked property is true (or use something like prototypes  
> $F() method which abstracts away that sort of concern.
> 
> Fred
