Sami
Sami

Reputation: 1051

How to use jQuery hide function in my Rails application?

How to use to jquery hide api in my rails application with the following block of code .. especially for the close button...

 <div class="loginwrapperinner">
  <div class="widgettitle">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <%= "<div class=\"flash_error\">#{h alert}</div>".html_safe unless alert.blank? %>
    <%= "<div class=\"flash_notice\">#{h notice}</div>".html_safe unless notice.blank? %>
  </div>
    .....
 </div>

Upvotes: 0

Views: 141

Answers (3)

Sami
Sami

Reputation: 1051

I found the following solution for my problem.

 $(function(){
$('.close').click(function(){
$(this).parent().hide();
});
});

Upvotes: 0

Kristian
Kristian

Reputation: 21800

A brief explanation may change the way you've thought of this problem:

  1. rails is generating html via the view. It is never (should never be) responsible for javascript.

  2. Inside of your javascript file(s) is where you'll write functionality like this.

So, lets say your view has generated a message box and that box's close button had a class of box_close_button, you could simply go into your JS file and attach a listener to that class:

$('.box_close_button').doSomeJavascriptHere(function() { //whatever you want. })

So, now its not a matter what to do in rails, but rather, whats the proper jquery selectors to do what you want

Upvotes: 0

Eraden
Eraden

Reputation: 2858

Cleaner version:

HTML

<div class="loginwrapperinner">
  <div class="widgettitle">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <!-- empty container will not appear (try to not using html_safe!) -->
    <div class="flash_error"><%= h(alert || '') %></div>
    <div class="flash_notice"><%= h(notice || '') %></div>
  </div>
    .....
 </div>

Javascript

$('.close').
  siblings('.flash_error, .flash_notice').hide().
  prevObject.parent().hide();

Upvotes: 1

Related Questions