How to traverse a children and a div with JavaScript?

5

I need a function that traverses #paicasas and leaves the background of all children "casa1, casa2 ...".

I tried to do this but I know it's far from right.

        function apagar(){
            for(var i in document.getElementById('paicasas')){
                document.getElementById('paicasas')[i].style.backgroundColor = "#FFF";
            }
        }

HTML

<div id="paicasas">
    <div id="casa1" onclick="mudarbg(this)"></div>
    <div id="casa2" onclick="mudarbg(this)"></div> 
    <div id="casa3" onclick="mudarbg(this)"></div> 
    </br>
    <div id="casa4" onclick="mudarbg(this)"></div> 
    <div id="casa5" onclick="mudarbg(this)"></div> 
    <div id="casa6" onclick="mudarbg(this)"></div> 
    </br>
    <div id="casa7" onclick="mudarbg(this)"></div> 
    <div id="casa8" onclick="mudarbg(this)"></div> 
    <div id="casa9" onclick="mudarbg(this)"></div> 
    <button type="button" onclick="apagar()">Novo</button>
</div>

Enjoying, I really need some stuff that explains about DOM.

    
asked by anonymous 10.11.2014 / 21:12

3 answers

3

I was almost right; you just forgot to traverse the array of children, rather than the element's attributes.

Additionally, to get only the DIVs, just check the value of tagName of each child.

See the final script below:

var pai = document.getElementById("paicasas");
for(var i = 0; i < pai.children.length; i++){
    if(pai.children[i].tagName == "DIV") pai.children[i].style.backgroundColor = "#FFFFFF";
}

For educational purposes only, what you were doing previously was to scan each attribute of the parent object. Notice what the console prints:

for(var i in document.getElementById("paicasas")) console.log(i);
/*
  align
  onautocompleteerror
  onautocomplete
  onwaiting
  onvolumechange
  ontoggle
  ontimeupdate
  onsuspend
  ...
  children
  ...
*/
    
10.11.2014 / 21:19
2

Try this out

document.getElementById('paicasas').children[i].style.backgroundColor = "#FFF";
    
10.11.2014 / 21:21
1

When you do for(var i in document.getElementById('paicasas')){ you will be going through all the properties of that object and not the elements that are your descendants.

I suggest using this:

function mudarbg(el) {
    el.style.backgroundColor = "#F0F";
}

function apagar() {
    var divs = document.querySelectorAll('#paicasas > [id^=casa]');
    for (var i = 0; i < divs.length; i++) {
        divs[i].style.backgroundColor = "#FFF";
    }
}
#paicasas > div {
    padding: 10px;
    margin: 1px;
    border: 1px solid black;
}
<div id="paicasas">
    <div id="casa1" onclick="mudarbg(this)"></div>
    <div id="casa2" onclick="mudarbg(this)"></div>
    <div id="casa3" onclick="mudarbg(this)"></div>
    <br/>
    <div id="casa4" onclick="mudarbg(this)"></div>
    <div id="casa5" onclick="mudarbg(this)"></div>
    <div id="casa6" onclick="mudarbg(this)"></div>
    <br/>
    <div id="casa7" onclick="mudarbg(this)"></div>
    <div id="casa8" onclick="mudarbg(this)"></div>
    <div id="casa9" onclick="mudarbg(this)"></div>
    <button type="button" onclick="apagar()">Novo</button>
</div>

[id^=casa] will select elements whose ID starts with casa .

    
09.12.2014 / 20:23