1

I want to do the following:

$("#dat_chk" + (i)).css("background-color", "#f6FFee");
$("#dat_opt" + (i)).css("background-color", "#f6FFee");
$("#dat_txt" + (i)).css("background-color", "#f6FFee");

Is these way I can shorten to just one select with jQuery?

JonAndMarie
  • 223
  • 1
  • 3
  • 7
  • is this a duplicate of http://stackoverflow.com/questions/1752718/how-do-i-select-more-than-one-element-using-the-jquery-attribute-selector ? – JMax Jun 22 '11 at 14:06
  • separate the selectors using commas and you can use it within a single jquery statement – abhijit Jun 22 '11 at 14:09

8 Answers8

3
$("#dat_chk" + i + ", #dat_opt" + i + ", #dat_txt" + i).css("background-color", "#f6ffee");
Ash Clarke
  • 4,807
  • 1
  • 37
  • 48
2
var selector = '#dat_chk' + i + ', #dat_opt' + i + ', #dat_txt' + i;

$(selector).css("background-color", "#f6FFee");
Alex
  • 34,899
  • 5
  • 77
  • 90
2

Try this

$("#dat_chk" + i + ", #dat_opt" + i + ", #dat_txt" + i).css("background-color", "#f6FFee");
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
1

Yes. Seperate them with a ,:

$("#dat_chk" + (i) + ", #dat_opt" + (i) + ", #dat_txt" + (i)).css("background-color", "#f6FFee");
Niklas
  • 29,752
  • 5
  • 50
  • 71
1

As well as using the comma separator, you can also use .add:

      $("#dat_chk" + i)
   .add("#dat_opt" + i)
   .add("#dat_txt" + i)
   .css( ... );
Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

the selector should look like this:

$( "#dat_chk" + i +  ", #dat_opt" + i + ", #dat_txt" + i)
Teneff
  • 30,564
  • 13
  • 72
  • 103
0

Easy buddy:

$("#dat_chk" + (i) + ", #dat_opt" + (i) + ", #dat_txt" + (i)).css("background-color", #f6FFee");

Like this example - http://jsfiddle.net/ajthomascouk/zNu3W/

Alex
  • 8,875
  • 2
  • 27
  • 44
-1
$('.class').css("background-color", "#f6FFee");
cetver
  • 11,279
  • 5
  • 36
  • 56