In the javascript code below, I wanted element el to point to a div with the ID “divNavyBox.” Here is the code for divNavyBox:
<div id="divNavyBox" class="box" onmouseover="animated.doAnimation()"></div>
The code below is the javascript used. Notice that an alert pops up with information about the type of el.
var animated = {
el : document.getElementById("divNavyBox"),
doAnimation : function() {
alert(typeof el);
if (el.className=="box") {
alert("2");
el.className="boxAlt";
}
if (el.className=="boxAlt") {
el.className="box";
}
}
};
Every time the alert pops up, it says that el is undefined. Why is it undefined when I declare it in the beginning of the code and assign it an element?
You are referring to
ellike it’s a global variable. You need to saythis.el