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,701
  • 36
  • 47
2
var selector = '#dat_chk' + i + ', #dat_opt' + i + ', #dat_txt' + i;

$(selector).css("background-color", "#f6FFee");
Alex
  • 34,121
  • 5
  • 74
  • 88
2

Try this

$("#dat_chk" + i + ", #dat_opt" + i + ", #dat_txt" + i).css("background-color", "#f6FFee");
ShankarSangoli
  • 68,720
  • 11
  • 89
  • 123
1

Yes. Seperate them with a ,:

$("#dat_chk" + (i) + ", #dat_opt" + (i) + ", #dat_txt" + (i)).css("background-color", "#f6FFee");
Niklas
  • 29,372
  • 5
  • 47
  • 69
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
  • 325,660
  • 70
  • 395
  • 481
0

the selector should look like this:

$( "#dat_chk" + i +  ", #dat_opt" + i + ", #dat_txt" + i)
Teneff
  • 26,872
  • 8
  • 62
  • 92
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,707
  • 2
  • 26
  • 44
-1
$('.class').css("background-color", "#f6FFee");
cetver
  • 9,889
  • 5
  • 34
  • 52