tags:

views:

149

answers:

4

I just want to control a div by toggling a class on click, as well as removing it when the mouse leaves the div.

I had it working with mouseout, but when I entered over a child element, the mouseOut triggered, so I know I need to use mouseleave. I am just unsure how incorporate telling jQuery the mouse has entered.

UPDATE: It seems functions won't work when I use mouseenter and mouseleave BUT they do work when I swap them to mouseover and mouseout.

Here is the site: http://danixd.com/test/

It has a lot on, but here is the code. I referred back to the old jquery I was using.

jQuery

$(function(){
            $('.main').click(function() { 
                $(this).toggleClass('activated');
            });
        });



$(function(){
            $('.main').mouseleave(function() { 
                $(this).removeClass('activated');
            });
        });
A: 

DEMO: http://jsbin.com/ipudo/9

try this:

you can trigger ( mouseleave ) event when another element is clicked:

$('.main').click(function(e) {
  e.preventDefault();
   $(this).addClass('activated');
  $('.main').mouseleave(function() {
 $(this).removeClass('activated');
 });
});

just for downvoters, the lines below are from jquery site:

$('#other').click(function() {
  $('#outer').mouseleave();
});
aSeptik
This is not a good idea, now you're adding an event handler with every click, why would you want to do this?
Nick Craver
The click works, but leave doesn't. Same problem as before.Does mouseleave require you to tell it has entered in the first place?
danixd
http://jsbin.com/ipudo/9
aSeptik
this just work like a charm! ;-)
aSeptik
@aSeptik - It doesn't, `$(".main").data("events").mouseleave.length`, see you're adding an event handler every click.
Nick Craver
http://api.jquery.com/mouseleave/
aSeptik
Your code works when I change mouseleave to mouseout, but mouseleave won't work, and I am stuck with the same problem as before. No idea why it isn't working.
danixd
Uh, if the link to the docs was directed at Nick Craver, I'm pretty sure he has the source code just about memorized... so not really necessary. This solution is just wrong.
patrick dw
@patrick: This solution is just wrong... this is just what he is asking! i well know that the correct way is another! but why you don't post a solution! this is just a try! ;-)
aSeptik
@aSeptik - You're confusing binding event handlers with firing them. Binding and unbinding as a means of simulating the effect of firing events is really bad practice.
patrick dw
wow it is started with +1 and finished with a downvoters coalition to -2 buhahuahha!
aSeptik
+1  A: 

I am just unsure how incorporate telling jQuery the mouse has entered.

If that is the case, then there is no need to handle the click event, from what I understand:

$('.main').mouseenter(function() { 
    $(this).addClass('activated');
}).mouseleave(function() { 
    $(this).removeClass('activated');
});
karim79
A: 

I think this is what you're looking for:

    $('.main').click(function() {
            if ($(this).attr("class").indexOf("activate") > -1) {
                $(this).removeClass("activate");
            } else {
                $(this).addClass("activate");
            }
    }).mouseleave(function() {
        $(this).removeClass("activate");
    });
Silkster
+3  A: 

If I understand, you want to add the class on 'click', and remove the class on 'click' OR 'mouseleave'.

If so, you could do something like this.

$('.main').click(function() { 
    var $ths = $(this);
    if($ths.hasClass('activated'))
        $ths.removeClass('activated');
    else
        $ths.addClass('activated');
}).mouseleave(function() { 
    $(this).removeClass('activated');
});
patrick dw
this is nice! ;-) `}).mouseleave` well done! i have never know that we can use it with click and mouselieave together! ;-) +1
aSeptik
They are really not together. They are two separate events assigned to `.main` that represent actions of the client. Is the same as doing `$('.main').click(func...` and `$('.main').mouseleave(func...`
patrick dw
you mean they are not like es: classic .mouseenter }).mouseleave ?
aSeptik
@aSeptik - I'm not exactly sure what your comment means. They are simply chained in jQuery fashion. They could easily be assigned separately as I indicated in my comment above. But there is nothing inherently 'together' about them. They are two separate events, just like `mouseenter` and `mouseleave` are two separate events. You can assign handlers to one, or both, or neither.
patrick dw