I just wrote some code that works very well. But I feel that it is “a little long”. I think I repeat my self.
Is there a way to optimize this?
Here is the code.
jQuery('#content article').mouseenter(function(){
var id = jQuery(this).attr('id');
var elHover = '#'+id+' .in_cat';
jQuery(elHover).removeClass('hidden');
});
jQuery('#content article').mouseout(function(){
var id = jQuery(this).attr('id');
var elHover = '#'+id+' .in_cat';
jQuery(elHover).addClass('hidden');
});
cordially
You can use
jQuery.hoverwhich can assign the same handler for both enter and leave. Then useJQuery.toggleClassinstead ofaddClass/removeClassor choose which action should be taken based on whathasClassreturns.With hover + toggleClass
With hover + conditional