USER10
USER10

Reputation: 974

how to add current class selected div in jquery

Codepen Demo

here i used hover function on jquery. it works correctly. but i need selected li on hover div.

$('.block-templates-container:not(:first-child)').hide();
   $(".block-name").each(function() {
      $(this).hover(function() {    
         $('.block-templates-container').hide();
         var getId = $(this).attr('id');
         $('.block-templates-container#' + getId + '_show').show();    
      });    
    });

when i hover rightside block(heading design,content design) the li not selected. how to fix this?

Upvotes: 1

Views: 55

Answers (2)

keziah
keziah

Reputation: 574

Try by adding class on hover

  //Sidebar 
  $('.block-templates-container:not(:first-child)').hide();
      $(this).hover(function() {
          $('.block-templates-container').hide();
          var getId = $(this).attr('id');
          $('.block-templates-container#' + getId + '_show').show();

          $('.block-name').not(this).removeClass('current');
          $(this).addClass('current');
      });
  });

Upvotes: 1

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

Try this: You don't need to use .each() and then put hover. also need not to use class selector withid, you can use id selector directly (id must be unique through out the DOM) - see below code

$(".block-name").hover(function() {
    $('.block-templates-container').hide();
    var getId = $(this).attr('id');
    $('#' + getId + '_show').show();
});

Upvotes: 0

Related Questions