tags:

views:

63

answers:

3

This works:

$('.overdue').addClass('alert');

But this doesn't:

$('.overdue').alert('Your book is overdue.'); 

What is the correct jQuery syntax for:

FOR EACH CLASS="overdue"
   alert('Your book is overdue');
NEXT
+7  A: 
$(".overdue").each( function() {
    alert("Your book is overdue.");
});

Note that ".addClass()" works because addClass is a function defined on the jQuery object. You can't just plop any old function on the end of a selector and expect it to work.

Also, probably a bad idea to bombard the user with n popups (where n = the number of books overdue).

Perhaps use the size function:

alert( "You have " + $(".overdue").size() + " books overdue." );
Adam Sills
Well, I EXPECTED it to work...
cf_PhillipSenn
It's okay. Spend some time on the JQuery website reading their documentation. It'll make sense what's going on eventually.
Adam Sills
+1  A: 

Hey,

For each works with JQuery as in

$(<selector>).each(function() {
   //this points to item
   alert('<msg>');
});

JQuery also, for a popup, has in the UI library a dialog widget: http://jqueryui.com/demos/dialog/

Check it out, works really well.

HTH.

Brian
How not to respond the question ^^
Clement Herreman
Agreed, i missed the point, and revised my comment.
Brian
Good alternative to use jQuery-UI.
cf_PhillipSenn
+3  A: 

Don't do this, but this is how you would do it:

$(".overdue").each(function() { 
    alert("Your book is overdue"); 
});

The reason I say "don't do it" is because nothing is more annoying to users, in my opinion, than repeated pop-ups that cannot be stopped. Instead, just use the length property and let them know that "You have X books overdue".

Sean Vieira
Annoying the user is what the client wants.
cf_PhillipSenn
@cf_PhillipSenn: *Chuckles* Understood.
Sean Vieira