javascript - Color Change not showing in Element? -
i'm trying grid working can use canvas. however, when try change color of elements(as test), nothing shows up. however, when give class dotted border(doesn't work solid, turns black) color trying change works. however, if remove border color goes away well. html:
<!doctype html> <!-- 4/28/15 graph part 2 --> <html lang="en"> <head> <title>this appear on tab in browser</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script src="graph.js" type="text/javascript"></script> <style type="text/css"> *{border: 0; margin:0; padding: 0; } body { margin:0;border:0;padding:0; font-family:"times new roman"; font-size: 12pt; } #header { width: 36em; height: 3em; border: 1px solid black; margin-left: auto; margin-right: auto; text-align: center; } #left { height: 21em; width: 7em; border: 1px solid black; margin-left: auto; margin-right: auto; position: relative; left: -223px; } #area { height: 21em; width: 21em; border: 1px solid black; margin-left: auto; margin-right: auto; position: relative; top: -338px; left: 2px; } #right { height: 21em; width: 7em; border: 1px solid black; margin-left: auto; margin-right: auto; position: relative; top: -677px; left: 228px; } #sketch { margin: 0px; font-size: 3em; } #small_box { height: 2em; width: 2em; border: 1px solid black; position: relative; top: -57px; left: 458px; font-size: 12pt; background-color: red; margin-top: 10px; } .box{border: 1px solid;} </style> </head> <body> <div id="page"> <div id="header"> <div id="sketch">click sketch <div id="small_box"></div> </div> </div> <div id="left"></div> <div id="area"></div> <div id="right"></div> </div> </body> </html>
js:
window.onload = function() { var array; var count; var html; var i; = 0; count = 0; array = new array(4); array[0] = getrandomrgb(); array[1] = getrandomrgb(); array[2] = getrandomrgb(); array[3] = getrandomrgb(); document.getelementbyid("small_box").style.backgroundcolor = getrandomrgb(); createcolorchoice(array, "left", "palette", updatecurrentcolor); createcolorchoice(array, "right", "canvas", updatecanvascolor); html = createdrawingarea("grid", 20, 30, "box", "startofrow"); document.getelementbyid("area").innerhtml = html; count = countelementswithidprefixof("grid"); (i = 0; < count; i++) { document.getelementbyid("grid" + i).style.backgroundcolor = "red"; } } function colorthebox(prefix, color) { this.style.backgroundcolor = document.getelementbyid("small_box").style.backgroundcolor; } function createdrawingarea(prefix, rows, columns, classid, start) { var classinfo; var count; var html; var i; var j; html = ""; count = 0; = 0; j = 0; (i = 0; < rows; = + 1) { classinfo = classid + " " + start; j = 0; (j = 0; j < columns; j = j + 1) { html = html + createhtmlelement("div", prefix + count, classinfo, ""); count = count + 1; j = j + 1; } = + 1; } return html; } function setcolorchoice() { var prompt; prompt = window.prompt("enter color: ") if (prompt.length > 0) { this.style.backgroundcolor = prompt; } } function updatecanvascolor() { document.getelementbyid("area").style.backgroundcolor = this.style.backgroundcolor; } function updatecurrentcolor() { document.getelementbyid("small_box").style.backgroundcolor = this.style.backgroundcolor; } function createcolorchoice(array, containerid, paletteprefix, reference) { var containerheight; var containerwidth; var i; var newhtml; = 0; newhtml = ""; containerid = "" + containerid; paletteprefix = "" + paletteprefix; containerheight = document.getelementbyid(containerid).offsetheight; containerwidth = document.getelementbyid(containerid).offsetwidth; (i = 0; < array.length; i++) { newhtml = newhtml + createhtmlelement("div", paletteprefix + i, "", ""); document.getelementbyid(containerid).innerhtml = newhtml; } (i = 0; < array.length; i++) { document.getelementbyid(paletteprefix + i).style.height = "3em"; document.getelementbyid(paletteprefix + i).style.width = "3em"; document.getelementbyid(paletteprefix + i).style.border = "1px solid black"; document.getelementbyid(paletteprefix + i).style.marginleft = "auto"; document.getelementbyid(paletteprefix + i).style.marginright = "auto"; document.getelementbyid(paletteprefix + i).style.margintop = "10px"; document.getelementbyid(paletteprefix + i).style.marginbottom = "5px"; document.getelementbyid(paletteprefix + i).style.backgroundcolor = array[i]; document.getelementbyid(paletteprefix + i).onmouseover = reference; document.getelementbyid(paletteprefix + i).onclick = setcolorchoice; } } function getcurrentcolor(id) { var element; element = document.getelementbyid(id) return element.style.backgroundcolor } function countelementswithidprefixof(prefix) { var count; var i; = 0; count = 0; while (document.getelementbyid(prefix + i) !== null) { count = count + 1; = + 1; } return count; } function getrandomrgb() // uses getrandominteger generate random rgb colors. { var red; var green; var blue; var result; red = getrandominteger(255); green = getrandominteger(255); blue = getrandominteger(255); result = "rgb(" + red + "," + green + "," + blue + ")"; return result; } function getrandominteger(upperlimit) // gets random number on less upperlimit { var result; result = math.random(); result = result * (upperlimit + 1); result = math.floor(result); return result; } function createhtmlelement(elementtype, id, classinfo, content) { if (elementtype == null) { elementtype = ""; } if (id == null) { id = ""; } if (classinfo == null) { classinfo = ""; } if (content == null) { content = ""; } else { elementtype = "" + trim(elementtype); id = ' id = ' + '"' + trim(id) + '"'; classinfo = ' class = ' + '"' + trim(classinfo) + '"'; content = "" + trim(content); } return "<" + elementtype + id + classinfo + ">" + content + "</" + elementtype + ">"; // returns formatted html statement. } function trim(data) //trim function removes whitespace string. { var result; var whitespace; var start; if (typeof data === "string") { whitespace = " \n\r\t\f"; start = 0; while (start < data.length && whitespace.indexof(data.charat(start)) > -1) { start = start + 1; } var end; end = data.length - 1; while (end >= 0 && whitespace.indexof(data.charat(end)) > -1) { end = end - 1; } if (end < start) { result = ""; } else { result = data.substring(start, end + 1); } } else { result = data; } return result; }
this issue is(i believe):
for (i = 0; < count; i++) { document.getelementbyid("grid" + i).style.backgroundcolor = "red"; }
you can either add content or can set width , height
.box{border: 1px solid; display: inline-block; width: 10px; height: 10px;}
see fiddle in action
Comments
Post a Comment