Jeff Pole
Jeff Pole

Reputation: 355

Javascript event handler for flash notice in Rails

I am just wondering if there is any way to do this?

Basically, I have some javascript that I want to execute after the user has closed the flash notice which appears when the page loads. Do I have to make a custom flash notice to get an event handler on it or is there a way to get a handler on the built-in rails flash notice?

Upvotes: 1

Views: 410

Answers (2)

user419017
user419017

Reputation:

This is assuming you're using coffeescript, which is currently default in Rails:

jQuery ->
  $('#flash_close_button').on 'click', (ev) ->
    # close flash notice
    # do other stuff here

This will require you to edit the html for the flash notices. To do this without an identifier, you might want to see this answer.

Upvotes: 0

ilan berci
ilan berci

Reputation: 3881

You can easily bind a jquery event handler to the user clicking on the button to dismiss the flash notice..

$().ready(function() {
  $("#button_id_that_user_clicks_to_dismiss_the_flash_notice").click(function() {
    alert("user has dismissed the flash notice")
  })
})

your flash element is probably located in app/views/layouts/application.html.erb .. edit this file to ensure that you properly id the dismissal button

Upvotes: 1

Related Questions