css - JavaScript Changing BG Color -


i made div background-color set rgb(0,0,0); , want change it's color on click javascript. made function that.

function change(){ var x = 1; var y = x + 100; document.getelementbyid("box").style.backgroundcolor = "rgb(" + y + "," + y + "," + y + ")"; } 

it works fine can change div's color once. want div's color value , set x , run function again. bg go black->grey->white on each click. depending on y variable.

i can div's value it'll in "rgb(0,0,0);" format. don't know after getting this. how manipulate integers in rgb(0,0,0); ?

you can store current x value in data attributes:

function change(box) {     var x = +box.getattribute('data-x'), // +box.dataset.x modern browsers         y = x + 100;     box.style.backgroundcolor = "rgb(" + y + "," + y + "," + y + ")";     box.setattribute('data-x', y); } 

html

<div id="box" onclick="change(this)"></div> 

demo: http://jsfiddle.net/wuz75/


Comments

Popular posts from this blog

Winapi c++: DialogBox hangs when breaking a loop -

vb.net - Font adding using PDFsharp -

javascript - jQuery iScroll clickable list elements while retaining scroll? -