0

So I need a little bit of help. I'm playing around with addClass and removeClass and I can't seem to remove a class after it's set. What I basically want is:

  1. When someone clicks an h3, it adds to its parent div class
  2. When someone clicks a div with added class, class needs to be removed

First step I got out of way and it's working

$(function(){
    $('div h3.itemTitle').on('click', function(){
        $(this).parent().addClass('active').siblings().removeClass('active');
    });
});

Now when I define:

$(function(){
    $('div.active').on('click', function(){
        $(this).removeClass('active');
    });
});

It does nothing, as if it doesn't see classes. It sets only those set in onload...

Help, anyone?

afaolek
  • 8,452
  • 13
  • 45
  • 60
Dino
  • 401
  • 4
  • 17
  • 1
    Duplicate? http://stackoverflow.com/a/8752376/125816 – Sergio Tulentsev Jul 23 '14 at 11:48
  • As indicated in the duplicate, that form of `on` only works with elements that are in the DOM at the time it executes. You should use the form that attaches to a parent element of the ones that should be affected and monitors changes in the DOM inside itself. – Klors Jul 23 '14 at 11:52
  • possible duplicate of [jQuery .live() vs .on() method for adding a click event after loading dynamic html](http://stackoverflow.com/questions/8752321/jquery-live-vs-on-method-for-adding-a-click-event-after-loading-dynamic-ht) – Klors Jul 23 '14 at 11:53
  • try to add some parent id or class of the active div – Shah Rukh Jul 23 '14 at 11:54
  • I'm not sure that link answers my particular question since I'm looking for solution, not necessary with .on('click'), but any solution that will work for me. – Dino Jul 23 '14 at 11:57
  • What about `$('h3.itemTitle').parent().on('click', function(){$(this).removeClass('active'))};`? Kinda long-winded, I know. – afaolek Jul 23 '14 at 11:59
  • Tried that, but no avail... – Dino Jul 23 '14 at 12:00

4 Answers4

3

The child element "h3.itemTitle" already had a click event listener on it and the parent can't actually capture the click event.

Your $('div.active').on('click', ...) never actually fires because you click the h3 not the div.

I recommend this approach: http://jsfiddle.net/c3Q6Q/

$('div h3.itemTitle').on('click', function () {
    // saves time not to write $(this).parent() everything so i store in a _parent var
    var _parent = $(this).parent();

    if (_parent.hasClass('active')) {
        _parent.removeClass('active');
    } else {
        _parent.addClass('active').siblings().removeClass('active');
    }
});
0

Try

 $('body').on('click','div.active', function(){$(this).removeClass('active');});

Instead of

$('div.active').on('click', function(){$(this).removeClass('active');});
0

I would go with this way:

$('div').on('click', function(e){
    var el = e.target;
    if($(el).is('h3') && $(el).hasClass('itemTitle')){
       $(this).parent().addClass('active').siblings().removeClass('active');
    }else if($(el).is('div') && $(el).hasClass('active')){
       $(this).removeClass('active');
    }
});
Jai
  • 74,255
  • 12
  • 74
  • 103
0

Not sure why every is talking about elements generated outside of the initial DOM load.

Here's a JSFiddle showing that it works: http://jsfiddle.net/H25bT/

Code:

$(document).ready(function() {
    $('.itemTitle').on('click', function() {
        $(this).parent().addClass('active').siblings().removeClass('active');
    });

    /* $('.parent').on('click', function() {
        $(this).removeClass('active');
    }); */

    $('.clicky').on('click', function() {
        $(this).parent().removeClass('active');
    });
});

The reason it's not working for you is that if you put the removeClass click event on the parent div itself, clicking on the child text causes a conflict with which click handler to use, and it won't work out. Code works fine if you don't assign the click to the parent div itself.

Meetarp
  • 2,331
  • 1
  • 23
  • 31