javascript - Store background color in a variable -
javascript - Store background color in a variable -
how store background color of 1 div in variable , later utilize variable alter background color of div using javascript?
var b2 = document.getelementbyid("box2"), b3 = document.getelementbyid("box3"); function d2(){ document.getelementbyid("box3").style.backgroundcolor = b2.style.backgroundcolor; document.getelementbyid("box2").style.backgroundcolor = b3.style.backgroundcolor; }
i made 2 boxes color reddish box2 , greenish box3. tried interchanging colors storing colors in variable , assigning color of variable divs didn't work. help appreciated. in advance.
this code worked fine me. help.
function getstyle(x,styleprop) { if (x.currentstyle) var y = x.currentstyle[styleprop]; else if (window.getcomputedstyle) var y = document.defaultview.getcomputedstyle(x,null).getpropertyvalue(styleprop); homecoming y; } function d2(){ b2 = document.getelementbyid("box2") b3 = document.getelementbyid("box3") y1 = getstyle ( b3, 'background-color' ) document.getelementbyid("box3").style.backgroundcolor = getstyle ( b2, 'background-color' ) document.getelementbyid("box2").style.backgroundcolor = y1 }
created fiddle you:
http://jsfiddle.net/jxyx2/
js:
var storedcolor = $('#one').css('background-color'); // set stored color 2 other div $('#two').css('background-color', storedcolor);
javascript
Comments
Post a Comment