Harry Moreno
Harry Moreno

Reputation: 11603

javascript namespace issue jsfiddle

I have code here http://jsfiddle.net/morenoh149/HWbuv/1/

this should be an easy fix. Why is itemClicked not being found?

my html 13" tortilla<input id="1" type="checkbox" onClick="itemClicked()"> <a id="console">the</a>

the relevant javascript

function itemClicked(element, id) {
if (element.checked) {
    list.push(item[id]);
    addItem(item[id]);
} else {
    for (x in list) {
        if (list[x].id == id) list.splice(x, 1);
    }
    subtractItem(item[id]);
}
var e = document.getElementById("console");
e.innerHTML = "blah";
}

Upvotes: 1

Views: 107

Answers (1)

Matt Zeunert
Matt Zeunert

Reputation: 16561

jsFiddle puts the code inside window.onload handler function, so if you declare a function in the Javascript panel it isn't global.

Make it global by assigning it to window:

function itemClicked(element, id) {
    /...
}
window.itemClicked = itemClicked; 

Or alternatively:

 window.itemClicked = function(element, id){...}

Upvotes: 4

Related Questions