views:

1549

answers:

2

I have two different methods that I want to execute when a user clicks a button. I have them working separately, but when I try to combine the two I get errors. The two ones I have currently are

                <%= image_tag('go_button.png',
                :id => "search_go_button",
                :class => "search_go_button",
                :onmouseover => "this.style.cursor='pointer';",
                :onmouseout => "this.style.cursor='default';",
                :onclick => "if ($(\"input_search_field\").value!=\"\" && $(\"input_search_field\").value!=\"Search Places\") {#{
                remote_function(:update => "right_nav;",
                :url => { :action => :search_places },
                :with => "'search_text='+$('input_search_field').value+'&search_radius='+$('radius').innerHTML",
                :before => "Element.show('search_spinner'); Element.hide('search_go_button');",
                :success => "Element.hide('search_spinner'); Element.show('search_go_button');")
                }}") %>

and

    :onclick => "collapse_reset(this); new Ajax.Request('/places/search/#{cat.id}?search_radius='+$('radius').innerHTML,{asynchronous:true, evalScripts:true}); {#{remote_function(
              :update => "localads;", 
              :url => { :action => :get_ads, 
                        :id => cat.id }, 
              :before => "Element.show('ad_search_spinner'); Element.show('ad_search_spinner1'); Element.show('ad_search_spinner2'); Element.hide('ad1'); Element.hide('ad2'); Element.hide('ad3'); if($('ad1_slide_down_wrap').style.display != 'none'){$('ad1_slide_down_wrap').style.display = 'none';} if($('ad2_slide_down_wrap').style.display != 'none'){$('ad2_slide_down_wrap').style.display = 'none';} if($('ad3_slide_down_wrap').style.display != 'none'){$('ad3_slide_down_wrap').style.display = 'none';}", 
              :success => "Element.hide('ad_search_spinner'); Element.hide('ad_search_spinner1'); Element.hide('ad_search_spinner2'); Element.show('ad1'); Element.show('ad2'); Element.show('ad3');") }}", 
              :href => "/places/navigate/#{cat.id}" } %>

I basically want add the function from the bottom one to the top one. Any help would be greatly appreciated.

+1  A: 

In the spirit of UJS (even if you're not providing non-javascript functionality here, you can benefit from separating the javascript from the erb), I would use Low Pro and set these up as behaviors. You'll need to download and include the lowpro.js file first.

In your .erb you would simply have the image tag:

<%= image_tag('go_button.png', 
              :id => "search_go_button", 
              :class => "search_go_button") %>

In an included .js file you might have something like this:

Event.addBehavior({
  '#search_go_button:click' : function(e) {
    // stuff you want to have happen in response to a click
  },

  '#search_go_button:mouseover' : function(e) {
    // stuff you want to have happen in response to a mouseover
  },

  '#search_go_button:mouseout' : function(e) {
    // stuff you want to have happen in response to a mouseout
  }

});

We use this for all our behaviors, and it makes the code much easier to work with and manage.

Post a comment if you need help sorting out the AJAX calls.

Abie
A: 

One (quick) way would be to try encapsulate the functions like this:

<script type="text/javascript">
function f1() {
if ($(\"input_search_field\").value!=\"\" && $(\"input_search_field\").value!=\"Search Places\") {#{
            <%= remote_function(:update => "right_nav;",
            :url => { :action => :search_places },
            :with => "'search_text='+$('input_search_field').value+'&search_radius='+$('radius').innerHTML",
            :before => "Element.show('search_spinner'); Element.hide('search_go_button');",
            :success => "Element.hide('search_spinner'); Element.show('search_go_button');")
            }} %>

}
function f2() {
    collapse_reset(this); new Ajax.Request('/places/search/#{cat.id}?search_radius='+$('radius').innerHTML,{asynchronous:true, evalScripts:true}); {#{ <%= remote_function(
          :update => "localads;", 
          :url => { :action => :get_ads, 
                    :id => cat.id }, 
          :before => "Element.show('ad_search_spinner'); Element.show('ad_search_spinner1'); Element.show('ad_search_spinner2'); Element.hide('ad1'); Element.hide('ad2'); Element.hide('ad3'); if($('ad1_slide_down_wrap').style.display != 'none'){$('ad1_slide_down_wrap').style.display = 'none';} if($('ad2_slide_down_wrap').style.display != 'none'){$('ad2_slide_down_wrap').style.display = 'none';} if($('ad3_slide_down_wrap').style.display != 'none'){$('ad3_slide_down_wrap').style.display = 'none';}", 
          :success => "Element.hide('ad_search_spinner'); Element.hide('ad_search_spinner1'); Element.hide('ad_search_spinner2'); Element.show('ad1'); Element.show('ad2'); Element.show('ad3');") }}", 
          :href => "/places/navigate/#{cat.id}" } %>
</script>

and:

 <%= image_tag('go_button.png',
            :id => "search_go_button",
            :class => "search_go_button",
            :onmouseover => "this.style.cursor='pointer';",
            :onmouseout => "this.style.cursor='default';",
            :onclick => "f1();f2();" %>

Maybe you need to pass variables into the functions, but this should work

Stefan Koenig