# Calling Javascript Function

**URL:** <https://www.ruby-forum.com/t/calling-javascript-function/131202>\
**Category:** Rails\
**Created:** [March 6, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/calling-javascript-function/131202 "2008-03-06T21:52:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 6, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/calling-javascript-function/131202/1 "2008-03-06T21:52:35Z")

</div>

Hello Everyone,

I was wondering if it’s possible to do the following:

I have an array of model objects(lets say items in my form) I would like  
to loop through this array and call a javascript function(that returns a  
boolean) on each element of the array. If true is returned I would then  
render a partial with the element. I am not sure how to call the java  
script function from the template as it is not triggered from any event.

Thanks,  
steve

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 7, 2008, 10:35am UTC](https://www.ruby-forum.com/t/calling-javascript-function/131202/2 "2008-03-07T10:35:20Z")

</div>

On 6 Mar 2008, at 20:52, Steve G. wrote:

> then  
> render a partial with the element. I am not sure how to call the java  
> script function from the template as it is not triggered from any  
> event.

This obviously has to happen client side, so when the conditions are  
met an ajax called is triggered.  
You say it is not triggered by any event, but that doesn’t really make  
sense: it’s bound to happen in response to something whether that on  
page load, when the user has populated the form, after some fixed  
amount of time has elapsed etc… Work out when the code should run  
(without thinking about the nitty gritty of the implementation) and  
work back from there.

Fred
