javascriptruby-on-railsrubyeruby

How to implement JS tag to fire on click in eruby code?


I need to get this piece of js to fire on a click event:

"_LT._trackEvent(_eventType.clickThrough)"

Need to add it to this eruby tag which already has an onclick event:

<%= submit_tag 'Send enquiry', :class => "BtnSubmit", :onclick => list_event(params[:item_key])%>

How would I go about doing this?


Solution

  • The onclick attribute (and its value) will be turned straight into an HTML attribute, so if you want to have it run both the callback in list_event and this other callback, you'll need to join them together in such a way that the JS will run both.

    I can see a few decent options for this:

    1. Modify the list_event helper to also run _LT._trackEvent(_eventType.clickThrough)
    2. Call your existing helper and tack the raw string on the end, as "#{list_event(params[:item_key])}; _LT._trackEvent(_eventType.clickThrough)"
    3. Create another helper called something like track_event and call them as "#{list_event(params[:item_key])}; #{track_event}"
    4. Create the track_event helper and then create a third helper to wrap them both, something like list_and_track_event, then call that instead

    Personally I'd go with options 1 or 4 depending on the semantics of the two callbacks.