javascript - Can't change element attribute inside a function -
javascript - Can't change element attribute inside a function -
i'm trying build simple drawing application in javascript. 1 requirement resize canvas according page, anyways, heres simplified reproduction of problem:
var maindiv = document.getelementbyid("fencemaker"); var maindivcs = window.getcomputedstyle(maindiv); var topcanvas = document.createelement("canvas"); topcanvas.style.backgroundcolor = "gray"; maindiv.appendchild(topcanvas); somefunction(); function somefunction() { topcanvas.width = 100; topcanvas.height = 100; }
i can alter width , height if set changes right after creating element within function nothing. if set "alert(topcanvas.width);" in function after changing value, returns right value (100) still draws wrong (defaults 300). whats going on here?
tested in opera 12 , chrome 35
it's need utilize lowercase width
, height
. should have:
function somefunction() { topcanvas.width = 100; topcanvas.height = 100; }
see jsfiddle: http://jsfiddle.net/5cc47/
javascript
Comments
Post a Comment