Form Field Observer - AJAX?

This is untested, but from the API something like:

observe_form ‘my_form’, :function => ‘recalulate()’

And define your recalculate method in a javascript file, eg application.js.

function recalculate() {

var value1 = Number($F(‘field_one’)) || 0;
var value2 = Number($F(‘field_two’)) || 0;
return value1 + value2;
}

You probably don’t need to observe the entire form though, you may be able to save some events by just observing the two fields you care about.

-Jonathan.

Brandon S. wrote:

Jonathan Viney wrote:
> This is untested, but from the API something like:
>
> observe_form 'my_form', :function => 'recalulate()'
>
> And define your recalculate method in a javascript file, eg
> application.js.
>
> function recalculate() {
> var value1 = Number($F('field_one')) || 0;
> var value2 = Number($F('field_two')) || 0;
> return value1 + value2;
> }
>
> You probably don't need to observe the entire form though, you may be
> able
> to save some events by just observing the two fields you care about.
>
> -Jonathan.

Jonathan,

Thanks for your help... I was able to write the recalculate function
like you said. However, once I return the sum from the function, I am
having trouble displaying it on the page. I have :update =>
'display_sum' as a parameter to my observe_form function, but the
display_sum <div> is not being updated with the sum. Am I doing
something wrong?

--
Posted via http://www.ruby-forum.com/.

Your custom function needs to do the actual page modification itself,
much like the RJS would have. In fact, an easy way to implement this
would be to use FireBug to see the RJS response of your previous
version and then just stuff the whole thing into a function with the
appropriate parameters replaced....

_Kevin

Get rid of :update => ‘display_sum’ and replace it with :function => ‘recalculate()’.

-Jonathan.