instance method link_to_function

Ruby on Rails 3.1.12

Since v2.2.3 Last seen in v4.0.13

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13

Signature

link_to_function(name, function, html_options={})

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>

Parameters

name req
function req
html_options opt = {}
Source
# File actionpack/lib/action_view/helpers/javascript_helper.rb, line 93
      def link_to_function(name, function, html_options={})
        onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;"
        href = html_options[:href] || '#'

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

Defined in actionpack/lib/action_view/helpers/javascript_helper.rb line 93 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionView::Helpers::JavaScriptHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close