Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  53] [ 5]  / answers: 1 / hits: 30778  / 11 Years ago, fri, october 11, 2013, 12:00:00

Currently I'm play with the html5 canvas, the simple code is below:



<!DOCTYPE HTML>
<html>
<head>
<title></title>
<script src=Scripts/jquery-2.0.3.min.js></script>
<script>
$(function () {
//THIS WILL NOT WORK!
//var cv = $(#cv);

//THIS WORKS FINE.
var cv = document.getElementById(cv);

ct = cv.getContext(2d);
var mText = hi;
var x = cv.width / 2;
var y = cv.height / 2;
ct.textAligh = center;
ct.fillText(mText, x, y);
});
</script>
</head>
<body>
<div style=width:200px; height:200px; margin:0 auto; padding:5px;>
<canvas id=cv width=200 height=200 style=border:2px solid black>
Your browser doesn't support html5! Please download a fitable browser.
</canvas>
</div>
</body>
</html>


The canvas element could only be picked by the method document.getElementById, but the jQuery method is not working. Is there a way to get the original html from the jquery object or am I miss using something?
Thanks in advance!


More From » jquery

 Answers
6

jQuery's $(<selector>) returns a collection of nodes (in fact it is object masquerades as an array as the doc says) so just use $('#cv').get(0) instead of document.getElementById(cv)


[#75053] Thursday, October 10, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mustafaericho

Total Points: 322
Total Questions: 103
Total Answers: 110

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
mustafaericho questions
Mon, May 31, 21, 00:00, 3 Years ago
Sun, May 23, 21, 00:00, 3 Years ago
Sat, Feb 13, 21, 00:00, 3 Years ago
Sat, Jan 2, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
;