user1559555
user1559555

Reputation: 167

Finding elements by attribute using jQuery

How can I find all elements in the document which have data-num attribute higher or equal to e.g. 251? I would prefer to use jQuery.

Upvotes: 0

Views: 98

Answers (2)

gen_Eric
gen_Eric

Reputation: 227180

You want to use the "has attribute" selector, then filter based on the value.

$('[data-num]').filter(function(){
    return parseInt($(this).data('num'), 10) >= 251;
});

DEMO: http://jsfiddle.net/xsvrv/

UPDATE: As per your comment, if you want to update the data-num value, you can do this:

$('[data-num]').filter(function(){
    var num = parseInt($(this).data('num'), 10);
    if(num >= 251){
        $(this).data('num', num+1);
        return true;
    }
    return false;
});

Upvotes: 2

Musa
Musa

Reputation: 97672

$('[data-num]').filter(function(){
    if (parseInt(this.getAttribute('data-num')) >= 251){
        return true;
    }
    else{
        return false;
    }
})

Upvotes: 1

Related Questions