This.name returns undefined in javascript

I am trying to remotely create onclick for each div (in order to save input time). Here is the window.onload function;

window.onload = function() {
            divel = document.getElementsByTagName('div');
            for(var el in divel){
                divel[el].onmouseover = function(){ this.style.textDecoration = "underline"; };
                divel[el].onmouseout = function(){ this.style.textDecoration = "none"; };
                divel[el].onclick = function(){ document.getElementById('game').src = "/games/" + this.name; };
            }
}

The name of each div is "flyingsheep" - this value has been set by the traditional <div name = "flyingsheep">

When I press the div, the iframe "game" brings me to the web page "/ games / undefined"

Thanks in advance.

(Tested in Google Chrome and Fennec (not the most ordinary browsers, I know))

+5
source share
3 answers

It will work. The problem is fixed.

just use: this.attributes["name"].value

window.onload = function() { 
        divel = document.getElementsByTagName('div');
        for(var el in divel){
        window.alert(divel[el].name);
            divel[el].onmouseover = function(){ this.style.textDecoration = "underline"; };
            divel[el].onmouseout = function(){ this.style.textDecoration = "none"; };
            divel[el].onclick = function(){document.getElementById('game').src = this.attributes["name"].value;} 
        }
}
+10
source

, , , , , , :

<div name="flyingsheep"> , . no name div.

, JS divel.length.onmouseover - for ( foo in bar ) , .

, div, , , ( ), , , ), JS.

+1

jquery :

$(this).attr("name");
0

All Articles