In jquery how to get a matching element with a defined property(attribute) value -
below jquery code, want li has data-target="5". loop not enter in if condition if there li data-target="5".
var target = 5; $("ul.menu-content li.collapsed").each(function() { if ($(this).is('[data-target]') == target) { $(this).removeclass('collapsed').find('a').addclass('active'); } });
you need data-target using .data(key) , compare value.
if($(this).data('target') == target) or, can directly use attribute value selector , code can improved as
$("ul.menu-content li.collapsed[data-target=" + target+"]") .removeclass('collapsed') .find('a') .addclass('active');
Comments
Post a Comment