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

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -