tags:

views:

40

answers:

3

I currently have a wordpress blog that assigns a number to each one of its posts.. It also takes this number and applies it to the container that the post sits in like so "post-813"

Is there a way with jQuery to where I can say.. If the class is post-810 or greater, add class "new" ??

Thanks

<div class="post-813 category-uncategorized">
...
</div>

Edit:

I was actually able to get it to print an ID like so:

<div id="post-810">

I was able to get the first ID, but now I need to do it for each. How is this possible?

 var number = $('div.post').attr("id");

 var trimmed = number.replace("post-", "");
A: 

I don't think there is one ready-made, but you can easily write a custom JQuery Filter for it. See filter() documentation and examples

Pekka
Right now I am trying to set the class as a variable, then stripping everything else out so I am left with a number.
Bruno
A filter would essentially do the same, it'd just be the more elegant way on the long run. Maybe one of our resident JQuery cracks comes along and can give you a starting point (I'm looking at you @Doug :)
Pekka
+1  A: 

You can make your own selector to help with this. Since you've got the divs assigned with Ids, it will make writing the selector a bit easier. First, add this to your code.

$.extend($.expr[':'], {
                post810: function(e) {
                    var id = $(e).attr("id");
                    var post = id.replace("post-", "");
                    return (post >= 810);
                }
            });

Then you can do this to add classes:

$(':post810').addClass('new')
wsanville
That worked like a charm! Thanks!
Bruno
Actually this only generates the class for the first instance in the blog. Is there a way to say check all posts instead of the first one?
Bruno
I'm not sure I understand. It's only applying the class for one post-xxx? I tried it out and it's selecting multiple divs with Ids of that format. What do you get if you do alert($(':post810').length) ?
wsanville
A: 

try this:

var patt = /post-(\d+)/g;
$("div[class*='post']").filter(function(){
    var result = patt.exec($(this).attr("class"));
    return result !== null && parseInt(result[1], 10) >= 810;
}).addClass("new");
Naeem Sarfraz