Neha Thakur Neha Thakur - 1 year ago 72
jQuery Question

In jquery how to get a matching element with a defined property(attribute) value

Below is my jQuery code, I want to get that

which has
But my loop not enter in if condition even if there is a

var target = 5;
$(" li.collapsed").each(function() {
if ($(this).is('[data-target]') == target) {

Answer Source

You need get data-target using .data(key) and compare its value.

if($(this).data('target') == target)

Or, You can directly use Attribute value selector and the code can be improved as

$(" li.collapsed[data-target=" + target+"]")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download