-1

I'm confused. Some of the basic jQuery method just doesn't work. When the other do.

It's my application.js file

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require bootstrap-toggle-buttons
//= require turbolinks
//= require_tree .

I have a modal, where I want to make some changes (add tick), when div is clicked.

This code works.

$(document).ready(function(){
   $( ".product-to-choose" ).click(function() {
       var icon = jQuery(this).find("i");
       icon.css("display","inline");
   });
 });

But when I want to add option to unchecked:

$(document).ready(function(){
   $( ".product-to-choose" ).click(function() {
       var icon = jQuery(this).find("i");
       icon.toggleClass('visible');
   });
 });

It doesn't work...

So I try do it another way:

$(document).ready(function(){
   $( ".product-to-choose" ).click(function() {
       var icon = jQuery(this).find("i");

       if(icon.hasClass('visible')){
           icon.removeClass('visible');
       }
       else{
           icon.addClass('visible');
       }
   });
});

Then it first added class, but when I clicked once again, nothing is gone. I have no idea what's going on. Is it any incompatibility between jQuery and Rails?

blazeP
  • 91
  • 9
  • What is your "visible" class? Are you trying to toggle between hide/show, e.g., `toggle()`? – Dave Newton Nov 24 '15 at 21:31
  • Yes, but it doesn't matter. If I understand it correct, toggleClass() method should join class from argument to html element, yes? And in my case it didn't happen. – blazeP Nov 24 '15 at 21:39
  • So for example icon.toggle(); do nothing. And icon.css("display","inline"); changes display value and icon is visible. Do you know why some methods work and other don't? – blazeP Nov 24 '15 at 21:44
  • Without a working example, e.g., a jsfiddle/etc, or the HTML, etc. it's impossible to help. HTML is HTML; jQuery doesn't care if it's coming from Rails or not. – Dave Newton Nov 24 '15 at 22:03

1 Answers1

0

After some research I know that problem is with integrating jQuery with turbolinks in Rails. I found this topic for example: Rails 4: how to use $(document).ready() with turbo-links

I hope it could be useful for someone, for me unfortunately not exactly. When I have code in script tags everything is ok. But when the js code is in application.js it doesn't work.

Community
  • 1
  • 1
blazeP
  • 91
  • 9