I have the following HTML…
<body onload="draw();">
<p><a href=""><canvas class="demo2" width="6" height="12">Fallback</canvas> Back to (1)...</a></p>
<p><a href=""><canvas class="demo2" width="6" height="12">Fallback</canvas> Back to (2)...</a></p>
</body>
…and javascript:
function draw() {
var canvas2 = $('.demo2').get(0); // This draws in the first canvas
//var canvas2 = $('.demo2').get(); This doesn't draw at all
if (canvas2.getContext) {
var ctx2 = canvas2.getContext('2d');
ctx2.beginPath();
ctx2.moveTo(6,0);
ctx2.lineTo(6,12);
ctx2.lineTo(0,6);
ctx2.fillStyle = 'rgb(0,100,220)';
ctx2.fill();
}
}
What I’d like to happen is for the all canvases with the class demo2 to be drawn in.
I thought $('.demo2').get() would get all elements by that classname. $('.demo2').get(0) draws in the first one but I’d like to draw in them all.
Demo at http://jsfiddle.net/kMN3s/
You can use
.eachto execute things for each.demo2: http://jsfiddle.net/kMN3s/2/.