There is a div with class set to col-md-7 and width attribute is not set.
I've to get the width of this div whenever the window is resized.
When I set width explicitly, I can access it in the resize event but when I don't set width and use col-md-7
, using element.getAttribute("width")
returns null
.
How can I solve this problem?
CODE
var canvas = document.getElementById('myCanvas');
var canvasHolder = document.getElementById("receiptHolder2");
initialize();
function initialize() {
window.addEventListener('resize', resizeCanvas, false);
resizeCanvas();
}
function redraw(){
var backgroundImage = new Image();
backgroundImage.onload = function(){
canvas.setAttribute("height",500);
canvas.setAttribute("width",300);
var context = canvas.getContext('2d');
alert(canvasHolder.width); // this line gives null
context.drawImage(backgroundImage,0,0,300,500);
}
backgroundImage.src="https://www.bhphotovideo.com/images/images500x500/Savage_1_12_107_x_12yds_Background_45468.jpg";
}
function resizeCanvas() {
canvas.width = canvasHolder.innerWidth + 30;
canvas.height = canvasHolder.innerHeight + 30;
redraw();
}