Method: ActionView::Helpers::JavaScriptHelper#button_to_function

Defined in:
lib/action_view/helpers/javascript_helper.rb

#button_to_function(name, *args, &block) ⇒ Object

Returns a button that’ll trigger a JavaScript function using the onclick handler.

The function argument can be omitted in favor of an update_page block, which evaluates to a string when the template is rendered (instead of making an Ajax request first).

Examples:

button_to_function "Greeting", "alert('Hello world!')"
button_to_function "Delete", "if (confirm('Really?')) do_delete()"
button_to_function "Details" do |page|
  page[:details].visual_effect :toggle_slide
end
button_to_function "Details", :class => "details_button" do |page|
  page[:details].visual_effect :toggle_slide
end


113
114
115
116
117
118
119
120
121
122
123
# File 'lib/action_view/helpers/javascript_helper.rb', line 113

def button_to_function(name, *args, &block)
  html_options = args.last.is_a?(Hash) ? args.pop : {}
  function = args[0] || ''

  html_options.symbolize_keys!
  function = update_page(&block) if block_given?
  tag(:input, html_options.merge({ 
    :type => "button", :value => name, 
    :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" 
  }))
end