Method: ActionView::Helpers::JavaScriptHelper#link_to_function

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

Returns a link whose onclick handler triggers the passed JavaScript.

The helper receives a name, JavaScript code, and an optional hash of HTML options. The name is used as the link text and the JavaScript code goes into the onclick attribute. If html_options has an :onclick, that one is put before function. Once all the JavaScript is set, the helper appends “; return false;”.

The href attribute of the tag is set to “#” unless html_options has one.

link_to_function "Greeting", "alert('Hello world!')", :class => "nav_link"
# => <a class="nav_link" href="#" onclick="alert('Hello world!'); return false;">Greeting</a>


104
105
106
107
108
109
110
111
# File 'lib/action_view/helpers/javascript_helper.rb', line 104

def link_to_function(name, function, html_options={})
  ActiveSupport::Deprecation.warn("link_to_function is deprecated and will be removed from Rails 4.0")

  onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;"
  href = html_options[:href] || '#'

  (:a, name, html_options.merge(:href => href, :onclick => onclick))
end