javascript - Change color of an id -
i'm new @ this. know if can me. wold chance color of id (a rectangle). want color change every 5 seconds colors have chosen , when clicked on rectangle, assume color @ time. thank you.
first of should select element using jquery id selector (http://api.jquery.com/id-selector/). example if element named "myelement" should write $("#myelement")
after selecting element can call jquery functions it. in case can call .css() function change background color propery of rectangle (may div element?). code $("#myelement").css("background-color", "red");
you can predefine colors in javascript array: var colors = ["red", "green", "blue"]; , create method called every 5 seconds (using javascript method setinterval) , change color.
i have wrote whole code you: http://jsfiddle.net/ww74t/
html:
<div id="myelement"></div>
css:
#myelement { width: 500px; height: 500px; }
javascript:
var colors = ["red", "green", "blue"]; var currentcolorindex = 0; $("#myelement").css("background-color", colors[currentcolorindex % colors.length]); setinterval(function(){ currentcolorindex++; $("#myelement").css("background-color", colors[currentcolorindex % colors.length]); },5000);
Comments
Post a Comment