0

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>

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

I found the following solution for my problem.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.