2

Please look at the following link to see my code in action:
http://codepen.io/DigitalSquid/pen/mAkuC

How can I make the bg color appear on page load?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Singh
  • 545
  • 2
  • 5
  • 24

2 Answers2

2

Call colourFunction() for each select at load time:

function initialColours() {
  colourFunction(document.getElementById('select1'));
  colourFunction(document.getElementById('select2'));
};

if (window.addEventListener) {
  window.addEventListener('load', initialColours, false);
}
// older IE versions have a non-standard variant, use that
else if (window.attachEvent) {
  window.attachEvent('onload', initialColours);
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
2

on window.load call that function with the selectbox ids..

try this

window.onload=function(){
 colourFunction(document.getElementById('select1'));
 colourFunction(document.getElementById('select2'));
};
bipen
  • 36,319
  • 9
  • 49
  • 62
  • i wish i could call both select together - maybe a general solution which would have applied to every select on in my code. thanks for this though :) – Singh May 15 '13 at 20:54
  • i wish you had used jquery there... :) .... http://jsfiddle.net/VaWZ7/1/ ..here is the example... – bipen May 15 '13 at 21:04
  • 1
    BTW, is there anything you cannot do? LOL - just kidding, i am going now, good night and thank you soo much! – Singh May 15 '13 at 21:09
  • i learnt all this from stackoverflow... :) :p... anyways cheers.. happy coding... – bipen May 15 '13 at 21:13
  • wow - i wish i become as smart as you one day - when i used this code in IE, after making the selection there is no blur i tried adding "$(this).blur;" which didn't work. what did i do wrong? i dont like when the selection is made and there is the default blue highlight rather than the bg color... – Singh May 15 '13 at 21:18
  • Actually once you choose every value - then the code stops working with bg colors – Singh May 15 '13 at 21:23
  • 1
    oppss... i forgot to add removeClass there..so it is just adding the classname there.... check this fiddle http://jsfiddle.net/VaWZ7/2/ – bipen May 15 '13 at 21:27
  • all good - cheers - programming is so funny, miss one little thing and you are in trouble ;) – Singh May 15 '13 at 21:31
  • 1
    The downsides to using `onload` (as opposed to `addEventListener`) is that you can only have a single event on load; if another script had already set window.onload, you've killed that handler. See http://stackoverflow.com/questions/6902033/element-onload-vs-element-addeventlistenerload-callbak-false – Paul Roub May 16 '13 at 01:30
  • @PaulRoub yes that is there.. :) – bipen May 16 '13 at 05:24