Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a partial from javascript

I have a select that gets populated depending on the selection in a different select To do this, I did what is recommended in Railscast #88 on 'Dynamic Select Menus'.

But now I need to render a partial, passing in the value selected in each of those selects. I can't figure out how to simply trigger a method call from the :onchange event in the select, so I'm thinking I need to call render :partial from within the javascript handler that detects the selects change event. But I can't figure out how. I tried the following in the javascript method (in the .js.erb file) but it doesn't work as hoped:

function staffSelected() {
    $('date_area').show();
    <% render :partial => "calendar" -%>
}

Anyone have a good solution?

like image 471
99miles Avatar asked Feb 25 '10 02:02

99miles


1 Answers

I think this is what you are looking for. I am rendering the partial within a DIV with "colors" ID, the partial is rendered only if the selected radio button's value is red:

<script language="javascript" type="text/javascript">
jQuery(function($){
  $('#radio-buttons input[type=radio]').click(function() {
    if ($(this).attr("value") === "red") {
      $("#colors").html('#{escape_javascript(render :partial => "colors")}');
    }
  });
});
</script>
like image 53
miligraf Avatar answered Oct 23 '22 03:10

miligraf