I have written a function which prints a certain area of the DOM which is triggered by a button click which is rendered via JavaScript. See the below code...
function MarkQuiz() {
var CorrectAnswers = 0,
TotalQuestions = 0,
CurrentQuestion = "",
Percentage = 0,
Output = "";
$("select").each(function(key,value) {
CurrentQuestion = "#" + $(value).attr("id");
TotalQuestions = key + 1;
if($(CurrentQuestion).val() == "Correct") {
CorrectAnswers++;
}
});
Percentage = (CorrectAnswers / TotalQuestions) * 100;
Output = "You Scored..." +
"<h1>"+Percentage+"%</h1>" +
"Which means you got " + CorrectAnswers + " out of " + TotalQuestions + " correct.<br/>" +
"<br/><a href='#' onclick='PrintCertificate('#QuizMainContent')' class='button' id='PrintCertificate'>Print your Certificate</a>";
$("#QuizMainContent").html(Output);
}
function PrintCertificate(DOMArea) {
var PrintArea = $(DOMArea),
PrintWindow = window.open('','');
PrintWindow.document.write($(PrintArea).html());
PrintWindow.document.close();
PrintWindow.focus();
PrintWindow.print();
PrintWindow.close();
}
However, when I click that button, I receive this error... enter image description here
This is the URL to the project: http://historicalperiods.esy.es/
What I am doing wrong? Thanks in Advance.
2 Answers 2
<a href="#" onclick="PrintCertificate(" #quizmaincontent')'="" class="button" id="PrintCertificate">Print your Certificate</a>
This is in your HTML. Which is what is causing the problem. As you can see, you open the attribute with ", then you use " in your javascript function, and you close the parameters in the function ', then you close the attribute with ', then you do ="" (which doesn't make sense).
The proper way it should be:
<a href="#" onclick="PrintCertificate('#quizmaincontent')" class="button" id="PrintCertificate">Print your Certificate</a>
2 Comments
onclick="" attribute, but also using " for the parameter in your function. Change the double quotes (") to single ones ('). Like this: onclick="PrintCertificate('#QuizMainContent')"Thanks for the help...
I corrected this with a little modification to the answer provided by L Ja...
'<br/><a href="#" onclick="PrintCertificate(\'#QuizMainContent\')" class="button" id="PrintCertificate">Print your Certificate</a>';
1 Comment
' inbetween " because it sees ' as part of the string. So: onclick=" ' " works. And onclick=" " " won't work, because the first " closes the attribute.
onclick='PrintCertificate('#QuizMainContent')'quotes?bohQuiz.html