Javascript woes

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’

12 thoughts on “Javascript woes

  1. Rob… yes it’s annoying – WordPress has a lot to answer for…. send email to pete at scargill dot org

  2. Or you could do this:

    $(‘div’).attr(‘style’, ‘background:rgb(‘+x[0]+’,’+x[1]+’,’+x[2]+’) !important’);

    1. 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.

      1. 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!

          1. 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!

            –>

      1. My pleasure. I’m usually a silent follower on your blog, but this one I could answer 🙂

Comments are closed.