# Fire JS event on page load

**URL:** <https://www.ruby-forum.com/t/fire-js-event-on-page-load/154392>\
**Category:** Rails\
**Created:** [November 28, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/fire-js-event-on-page-load/154392 "2008-11-28T16:23:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [November 28, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/fire-js-event-on-page-load/154392/1 "2008-11-28T16:23:10Z")

</div>

Hi,

I have a drop down select which fires an ajax call once selected.  
So when i select anything in the email\_seperator drop down it fisre the  
ajax event below.

\<%= observe\_field(‘email\_seperator’,  
:on =\> ‘click’,  
:url =\> {:controller =\> ‘possible\_emails’, :action =\>  
‘update\_email’},  
:with =\> “‘email\_seperator=’+$F(‘email\_seperator’)”,  
:before =\> “Element.show(‘spinner’)”,  
:success =\> “Element.hide(‘spinner’)”) %\>

What i want is when the page first loads for this ajax event to fire  
once so the correct information is diaplyed. Ive tried something like  
the below but it works for focus but not for click

\<%= javascript\_tag render(:update) { |page|  
page[‘email\_seperator’].focus } %\>

\<%= javascript\_tag render(:update) { |page|  
page[‘email\_seperator’].click } %\>

Can anyone help??

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [November 28, 2008, 8:58pm UTC](https://www.ruby-forum.com/t/fire-js-event-on-page-load/154392/2 "2008-11-28T20:58:17Z")

</div>

Hi John,

John B. wrote:

> ```
> :before => "Element.show('spinner')",
> :success => "Element.hide('spinner')") %>
> 
> ```
> 
> What i want is when the page first loads for this ajax event to fire  
> once so the correct information is diaplyed.

What does ‘so the correct information is displayed [sic]’ mean? If you  
mean  
you want to give the dropdown an initial value, options\_for\_select lets  
you  
specify the selected value. See the documentation for that method and  
the  
ones that follow it at [api.rubyonrails.org](http://api.rubyonrails.org).

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [November 29, 2008, 11:34am UTC](https://www.ruby-forum.com/t/fire-js-event-on-page-load/154392/3 "2008-11-29T11:34:11Z")

</div>

Bill W. wrote:

> Hi John,
> 
> John B. wrote:
> 
> > ```
> > :before => "Element.show('spinner')",
> > :success => "Element.hide('spinner')") %>
> > 
> > ```
> > 
> > What i want is when the page first loads for this ajax event to fire  
> > once so the correct information is diaplyed.
> 
> What does ‘so the correct information is displayed [sic]’ mean? If you  
> mean  
> you want to give the dropdown an initial value, options\_for\_select lets  
> you  
> specify the selected value. See the documentation for that method and  
> the  
> ones that follow it at [api.rubyonrails.org](http://api.rubyonrails.org).
> 
> HTH,  
> Bill

Yes i have the correct value displaying in the drop down but this then  
fires an ajax request when clicked to update some components on the  
view. Id like to automaticall fire this event when the page loads??

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [November 29, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/fire-js-event-on-page-load/154392/4 "2008-11-29T17:53:54Z")

</div>

On Sat, Nov 29, 2008 at 2:34 AM, John B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yes i have the correct value displaying in the drop down but this then  
> fires an ajax request when clicked to update some components on the  
> view. Id like to automaticall fire this event when the page loads??

That – rendering a page and immediately changing it – makes no  
sense at all; why are you not delivering your page with the proper  
initial state?

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid
