Leke
Leke

Reputation: 883

Why isn't my function defined?

How come this returns an error? In my head tag...

var movieArray = [
["Everything is Illuminated", "0", ""],
["The Girl Who Leapt Through Time (Toki wo kakeru shojo)", "1", "<ol><li><span class=\"bold quote_actor\">Kosuke Yoshiyama: </span><span class=\"line\">It&#039;s not that rare. Many girls do it at your age.</span></li>                                                </ol>"],
["Freedom Writers", "0", ""],
["Inside Man", "0", ""]
];

function checkAnswer(this.value) {
    if (this.value == 0) {
        alert ("Wrong answer!");
    } else {
        alert ("Correct Answer!");
    }

}

In the body...

<p><a id="0" class="btn btn-primary btn-large" value="0" onclick="checkAnswer(this.value)">Everything is Illuminated</a></p>
<p><a id="1" class="btn btn-primary btn-large" value="1" onclick="checkAnswer(this.value)">The Girl Who Leapt Through Time (Toki wo kakeru shojo)</a></p>

The error is checkAnswer() is not defined. How come?

Thanks.

Upvotes: 0

Views: 581

Answers (3)

Atif
Atif

Reputation: 10880

You forgot to escape " inside array elements by adding backslash

var movieArray = [
    ["Everything is Illuminated", "0", ""],
    ["The Girl Who Leapt Through Time (Toki wo kakeru shojo)", "1", "<ol><li><span class=\"bold quote_actor\">Kosuke Yoshiyama: </span><span class=\"line\">It&#039;s not that rare. Many girls do it at your age.</span></li>                                                </ol>"],
    ["Freedom Writers", "0", ""],
    ["Inside Man", "0", ""]
    ];

function checkAnswer(arg1) {
    if (arg1 == 0) {
        alert("Wrong answer!");
    } else {
        alert("Correct Answer!");
    }

}​

Secondly this.value should be passed while calling the checkAnswer() and not at the time of defitinion

Upvotes: 3

kannanrbk
kannanrbk

Reputation: 7134

Error in your function syntax .

Error :

function checkAnswer(this.value) {

A function syntax must be a functionName(formalParameterName) . You are passing a value in a object in a function parameter this will lead into error . You have to change this to function checkAnswer(param) .

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

How did everyone miss the this.value in the arguments list? It should be an identifier, such as myvalue. This is the reason why checkAnswer is not defined.

Now, as for the rest of the code, you can't use this in the checkAnswer function as it will refer to the global object, not the link. Also, links don't support the value property, so you need to use getAttribute or just put the value in the onClick event outright.

function checkAnswer(myval) {
    if (myval == 0) {
        alert ("Wrong answer!");
    } else {
        alert ("Correct Answer!");
    }
}

<p><a class="btn btn-primary btn-large" onclick="checkAnswer(0)">Everything is Illuminated</a></p>
<p><a class="btn btn-primary btn-large" onclick="checkAnswer(1)">The Girl Who Leapt Through Time (Toki wo kakeru shojo)</a></p>

I also removed the id attributes, since an ID starting with a number is not valid except in HTML5 (and even then they're not a good idea due to older browsers still in widespread use, and even without that they're meaningless)

Upvotes: 2

Related Questions