This works:

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

But this doesn't:

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

What is the correct jQuery syntax for:

FOR EACH alert('Your book is overdue'); NEXT 
3

3 Answers

$(".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." ); 
1

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".

1

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:

Check it out, works really well.

HTH.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy