Someone put me out of my misery… why doesn’t this work…
var x=[];
x[0]=255;
x[1]=255;
x[2]=0;
var t=x[0].toString(16)+x[1].toString(16)+x[2].toString(16);
alert(t);
$(‘div’).attr(‘style’, ‘background:#’+t+’ !important’);
I’m changing the colour of some panels…
THIS works..
$(‘div’).attr(‘style’, ‘background:#ffff00 !important’);
but THIS doesn’t
$(‘div’).attr(‘style’, ‘background:#’+t+’ !important’);
even though t=’ffff00’
Thank you for the comments – all sorted and suitable enlightened.
Rob… yes it’s annoying – WordPress has a lot to answer for…. send email to pete at scargill dot org
Or you could do this:
$(‘div’).attr(‘style’, ‘background:rgb(‘+x[0]+’,’+x[1]+’,’+x[2]+’) !important’);
I like your version Rob… but it doesn’t work.
Here you’ll see I’ve commented out what does work – put your solution in – and that doesn’t work… could it be that a change is needed as it is a specific ID I’m targetting?
//var t=(“0″+m[0].toString(16)).slice(-2)+(“0″+m[1].toString(16)).slice(-2)+(“0″+m[2].toString(16)).slice(-2);
// $(‘#mydiv3’).css(‘backgroundColor’,’#’+t);
$(‘#mydiv3’).attr(‘style’, ‘background:rgb(‘+m[0]+’,’+m[1]+’,’+m[2]+’) !important’);
Ignore the special quotes that WordPress has just kindly introduced.
Hmm. Well, this complete example works (in Chrome and Safari, at least):
function doIt() {
var m=[];
m[0]=255;
m[1]=255;
m[2]=0;
$(‘#mydiv3’).attr(‘style’, ‘background:rgb(‘+m[0]+’,’+m[1]+’,’+m[2]+’) !important’);
}
Colour me in
Do it!
Argh, you can’t copy and paste HTML into a reply…
Try again:
<!–
function doIt() {
var m=[];
m[0]=255;
m[1]=255;
m[2]=0;
$(‘#mydiv3’).attr(‘style’, ‘background:rgb(‘+m[0]+’,’+m[1]+’,’+m[2]+’) !important’);
}
Colour me in
Do it!
–>
[Sigh]
Can I email my example to you…?
t isn’t ‘ffff00’. It’s ‘ffff0’, an invalid colour value.
Thanks for that Peter – I cracked it….
thats what i did:
if (grey <= 16) grey = '0'+grey.toString(16);
else grey = grey.toString(16);
My pleasure. I’m usually a silent follower on your blog, but this one I could answer 🙂