jquery -- Can anyone improve my syntax to combine selectors?

ShanghaiD used Ask the Experts™
I have code which works, but I am trying to improve it (and learn more about jQuery)!

My current code selects images inside a <td> column in a table, where some <img> have class='add' and some have class='view'.

For the class='add' it then filters depending on the text value in the previous <td> column.

For the class='add' it discards -- using not() -- based on an internal function -- daysBetween() -- being applied to a data item attached to the row.

Both of these separately filtered selections then have the same class applied to them (to change the background colour of the column) and the same effect on the <img>. -- to make it pulsate.

My current code is as follows:

     $('.add').filter(function() {return parseInt($(this).parent().prev().text())>120}).effect('pulsate', 'slow').parent().addClass('bgRed');

      $('.view').not(function() {return daysBetween($(this).parent().parent().data('lastedit'))<15}).effect('pulsate', 'slow').parent().addClass('bgRed');

My question is, how can the two selector criteria be combined, so the addClass() and effect() can then be applied once to everything?  

In other words, how do I combine
$('.add').filter(function() {return parseInt($(this).parent().prev().text())>120})
$('.view').not(function() {return daysBetween($(this).parent().parent().data('lastedit'))<15})
and then apply
.effect('pulsate', 'slow').parent().addClass('bgRed');
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Although i'm used to a way of approach like yours , you can try this one.

$('img').each(function (){
if ($(this).hasClass('.add') && $(this).parent().prev().text()) > 120) ||
    ( $(this.hasClass('.view') && $(this).parent().parent().data('lastedit')) < 15)){
$(this).effect('pulsate', 'slow').parent().addClass('bgRed');

please feedback
Information Systems Manager
Top Expert 2012
I would add to this so that the test doesn't happen for all images:

$('img.add,img.view').each(function (){
if ($(this).hasClass('.add') && $(this).parent().prev().text()) > 120) ||
    ( $(this.hasClass('.view') && $(this).parent().parent().data('lastedit')) < 15)){
$(this).effect('pulsate', 'slow').parent().addClass('bgRed');

Open in new window


Thank you (both) for pointing me in the right direction (and expanding my jQuery knowledge).  I've awarded most points to stergium for his quick response but I also appreciated the fine-tuning from JonNorman.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial