Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQquery - the elements first class was changed, but jquery still calls the elements first class.

I have a problem and I really searched for answers and I asked some persons.. but no solution. I have 3 'td' elements, they all belong to a class = "moreSpace" ... so when I click on a td element.. it changes its class to "deactivateAssignments" ... but the $(".moreSpace").mouseOver/mouseOut functions still have an effect on the element which its class was changed.. Why? please help me :(

(sorry for this mess, it is the first time that I post a problem here)

//DG

$(".moreSpace").mouseover(function(event) {
    var id = event.target.id;
    $('#'+id).css('color','#B5EABC')
});

$(".moreSpace").mouseout(function(event) {
    var id = event.target.id;
    $('#'+id).css('color','#24C635')
});

 function reclass(){
  $('#dg306').attr('class','moreSpace');
  $('#dg632').attr('class','moreSpace');
  $('#dg291').attr('class','moreSpace');
  alert('in');

}

var actId = 2;
$(".moreSpace").click(function(event) {
  reclass();
  var id = event.target.id;
  if (id == 'dg306'){
     actId = 1;
  } else if (id == 'dg632'){
     actId = 2;
  }else if (id == 'dg291'){
     actId = 3;
  }



switch (actId) {
       case 1:
            $('#dg306').toggleClass('moreSpace deactivateAssignments');
            break;
       case 2:
            $('#dg632').toggleClass('moreSpace deactivateAssignments');
            break;
       case 3:
            $('#dg291').toggleClass('moreSpace deactivateAssignments');
            break;

  }
like image 1000
MisterNowhere Avatar asked Dec 07 '25 05:12

MisterNowhere


1 Answers

You are using direct binding on the element. So whatever you are changing on this element, the binding will still be there.

2 solutions. You can unbind the binding with off inside the mouse event :

$(this).off('mouseover mouseout');

Or use delegation :

$(document).on({
    mouseover : function(){},
    mouseout : function(){}
}, '.moreSpace');
like image 173
Karl-André Gagnon Avatar answered Dec 08 '25 20:12

Karl-André Gagnon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!