tags:

views:

24

answers:

2

I would like to fade all divs that have a numeric attribute greater than or less than a given number.

Something like $( "div[id$=2]" ).fadeTo("slow", 0.6);

but I would like to use the ">" or "<" operator instead of "="

basically I would name all my divs something like

<div id="22">text</div>
<div id="35">text</div>
<div id="40">text</div>

and then use

$( "div[id$>35]" ).fadeTo("slow", 0.6); 

to fade out the last cell

+2  A: 

This isn't valid HTML. An id may not start with a digit. Instead, you may find the :gt selector helpful. You can use, e.g. td:gt(4) to find those tds with 0-index > 4, or #container div:gt(1) to find divs in container with index > 1.

Matthew Flaschen
The only problem with using :gt is that it assumes that the numbered divs appear in numerical order on the page which may/may not be the case.
Russ Cam
+1  A: 

First of all, your HTML is not valid. ID attribute should not start with a number.

do it like this,

<div id="div22" class="fade">text</div>
<div id="div35" class="fade">text</div>
<div id="div40" class="fade">text</div>

then jQuery

var divs = $('.fade').map(function(){
                if (this.id.replace('div','') > 35) return '#'+this.id;
           }).get().join(',');

$(divs).fadeTo("slow", 0.6); 

You may play it here.

this works too,

var divs = $('.fade').map(function(){
            if (this.id.replace('div','') > 22) return this;
       }).get();
$(divs).fadeOut("slow"); 

as Russ Cam suggested to use filter,

var divs = $('.fade').filter(function(){
            return (this.id.replace('div','') > 22);
       });
$(divs).fadeOut("slow"); 
Reigel
Using filter would probably be easier
Russ Cam