0

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.

10
  • kindly share URL as it's not issue with file but with URL Commented Dec 10, 2015 at 11:30
  • Well, what code comes up when you click the handy link it gave you? Commented Dec 10, 2015 at 11:30
  • The error message says the error comes from a different file. Click the link in the console to go to that file. Commented Dec 10, 2015 at 11:31
  • 5
    onclick='PrintCertificate('#QuizMainContent')' quotes? Commented Dec 10, 2015 at 11:31
  • Can you provide the code from bohQuiz.html Commented Dec 10, 2015 at 11:31

2 Answers 2

0
<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>
Sign up to request clarification or add additional context in comments.

2 Comments

I am still getting this error. This is what I've done... '<br/><a href="#" onclick="PrintCertificate("#QuizMainContent")" class="button"" id="PrintCertificate">Print your Certificate</a>';
You are still opening the onclick="" attribute, but also using " for the parameter in your function. Change the double quotes (") to single ones ('). Like this: onclick="PrintCertificate('#QuizMainContent')"
0

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

You don't have to escape the parameter. Since you can use ' inbetween " because it sees ' as part of the string. So: onclick=" ' " works. And onclick=" " " won't work, because the first " closes the attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.