I am trying to remotely create an onclick
for each <div>
(to save typing time).
Here is the window.onload()
function:
JavaScript
x
15
15
1
window.onload = function() {
2
divel = document.getElementsByTagName('div');
3
for (var el in divel) {
4
divel[el].onmouseover = function() {
5
this.style.textDecoration = "underline";
6
};
7
divel[el].onmouseout = function() {
8
this.style.textDecoration = "none";
9
};
10
divel[el].onclick = function() {
11
document.getElementById('game').src = "/games/" + this.name;
12
};
13
}
14
}
15
The name of every <div>
is "flyingsheep"
– this value was set by <div name="flyingsheep">
.
When I click the <div>
, the iframe "game"
takes me to the webpage "/games/undefined"
.
Advertisement
Answer
This will work. the problem is corrected.
just use : this.attributes["name"].value
JavaScript
1
10
10
1
window.onload = function() {
2
divel = document.getElementsByTagName('div');
3
for(var el in divel){
4
window.alert(divel[el].name);
5
divel[el].onmouseover = function(){ this.style.textDecoration = "underline"; };
6
divel[el].onmouseout = function(){ this.style.textDecoration = "none"; };
7
divel[el].onclick = function(){document.getElementById('game').src = this.attributes["name"].value;}
8
}
9
}
10