3

quick one for you. I looked at the selectors in jQuery.com and could not find a way to filter by selector that is greater than a number? I want to do this:

$("[level>'4']").hide();

My html looks like this:

<div id="1" level="4">Test</div>

how can I hide all divs greater than 4 using that or something like that syntax?4

Paul
  • 1,527
  • 2
  • 16
  • 24
  • You'd be better off using html 5 `data-` attributes instead of non standard html attributes. E.g. `data-level="4"` instead of `level="4"`. – jrummell Apr 27 '12 at 19:38

4 Answers4

6

Try below,

$("[level]").filter(function () {
   return parseInt($(this).attr('level'), 10) > 4;
}).hide();
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
4
$('div').filter(function(){
  return parseInt($(this).attr('level')) > 4);
});
james
  • 170
  • 1
  • 3
  • 9
1

Simply do this :

$('[level]')​.each(function(){
var $this=$(ths);
var level = parseInt($this.attr('level'), 10);
if (level>4) $this.hide();
});​
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

I needed this particular functionality too where the check is done in the selector rather than as another embedded function. Maybe I did more than I needed to, but it means I can now filter date-based stuff with Isotope using a Selector rather than having to write stuff in each filter call.

What I wrote uses the pseudo-selector stuff with jQuery.

// Hides elements with the attribute "index" that is greater than 4
$(':attrGT("index",4)').hide();

// Filter elements using Isotope with the attribute "data-starttime" that is less than or equal to 1234567890
$container.isotope({
    filter: ':attrLTEq("data-starttime",1234567890)'
});

Here's the initial code for version 1 (I haven't done ANY extensive testing at all, but so far it has worked for me):

(function ($) {
    // Single function to do all the heavy lifting
    function attrGTLTSelector(mode, obj, meta) {
        var args,
            objAttr,
            checkAttr,
            output = false;

        if (typeof meta === 'object') {
            args = meta;
        } else {
            if (meta.match(',')) {
                args = meta.split(/["'\s]*,["'\s]*/);
            } else {
                args = [meta];
            }
        }

        objAttr = parseInt($(obj).attr(args[0]), 10);
        checkAttr = parseInt(args[1], 10);

        switch (mode) {
        case 'lt':
            if (objAttr<checkAttr) output = true;
            break;
        case 'lte':
            if (objAttr<=checkAttr) output = true;
            break;
        case 'gt':
            if (objAttr>checkAttr) output = true;
            break;
        case 'gte':
            if (objAttr>=checkAttr) output = true;
            break;
        }

        if (window.console) if (console.log) console.log('attrGTLTSelector', objAttr, mode, checkAttr, output);

        return output;
    }

    // Add custom pseudo selectors to jQuery
    $.expr[':'].attrLT = function(obj, index, meta, stack){
        return attrGTLTSelector('lt', obj, meta[3]);
    };
    $.expr[':'].attrGT = function(obj, index, meta, stack){
        return attrGTLTSelector('gt', obj, meta[3]);
    };
    $.expr[':'].attrLTEq = function(obj, index, meta, stack){
        return attrGTLTSelector('lte', obj, meta[3]);
    };
    $.expr[':'].attrGTEq = function(obj, index, meta, stack){
        return attrGTLTSelector('gte', obj, meta[3]);
    };
}(jQuery));
Matt Scheurich
  • 959
  • 2
  • 12
  • 24