this.name vrací v javascriptu hodnotu undefined

Snažím se vzdáleně vytvořit onclick za každých <div> (pro úsporu času při psaní).

Zde je window.onload() funkce:

  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;
      };
    }
  }

Název každého <div> je "flyingsheep" – tato hodnota byla nastavena pomocí <div name="flyingsheep"> .

Když kliknu na <div> , prvek iframe "game" přesměruje mě na webovou stránku "/games/undefined" .

Odpověď

To bude fungovat. problém je opraven.

stačí použít :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;} 
        }
}