Possible Duplicate:
jQuery equivalent of getting the context of a Canvas
My Questions are:
1) Why does the commented code not behave like that above it (i.e why does it throw an error)?
$(function () {
$('#ex4').append('<canvas class=can ></canvas>');
$('#ex4').append('<canvas class=can ></canvas>');
$('#ex4').append('<canvas class=can ></canvas>');
$('#ex4 canvas').each(function (index, element) {
$(this).attr("width", "125")
$(this).attr("height", "50")
var context = element.getContext("2d");
//var context = $(this).getContext("2d"); error
context.strokeStyle = "red";
context.fillStyle = "#999900";
context.font = "30px Arial";
context.fillText("HTML5", 0, 35);
});
})
2) How do I resolve the error (presumably the same as that above) in the analogous code below?
$.each(data[0], function (i) {
$('#content').append('<canvas id=' + i + ' class=can ></canvas>');
$('#content #' + i).attr("width", "125")
$('#content #' + i).attr("height", "50")
//var context = $('#content #'+i).getContext("2d"); error
})