2
Well, I’m new to using Javascript and I’m having some difficulties. I have a box that has 6 links, this involved in a gift-repeat, IE, are many 'boxes' with 6 links inside. I need to check if inside each box there is a link, if there is one I can already leave the box visible, but if all are null or empty the box should disappear. Unfortunately, I’m not being able to think of a logic good enough to get what I need. Besides, I’m having a hard time getting the value of href.
I tried to create a code, but besides not being working, I think I’m thinking badly on how to solve the problem, I seem to lack something to analyze all 6.
Function I created to try to solve:
function teste(){
var div = document.getElementsByClassName('box');
var links = div.getElementsByTagName('a').href;
if(links == '#'){
div.style.display = 'none'
}
}
I set up a second function, but it didn’t work either
function teste2(){
var box = document.getElementsByClassName('box');
var link = box.getElementsByTagName('a').href;
var contar = 0;
for (x = 0; x < 6; x++){
if(link == "#"){
contar++
}
}
if (contar == 6){
box.style.display = 'none'
}
}
HTML base
<div class="box">
<a href="#">A</a>
<a href="#">B</a>
<a href="#">C</a>
<a href="#">D</a>
<a href="#">E</a>
<a href="#">F</a>
</div>
getElementsByClassName
andgetElementsByTagName
return arrays (and not a single element), so you must go through these arrays to access the returned elements– hkotsubo