-1

I've this following HTML:

<li id="telephone"><a href="tel:938409"target="_blank"><img  src="https://playground.air-srl.com/icombanner/telico.png"/></a></li>
                    <li id="ico"><a href="./cmbmodal.php?servizio=TEST" target="_blank"><img src="https://playground.air-srl.com/icombanner/cmbico.png"/></a></li>
                    <li id="status"></li>
                    <li  id="icon"><a href="https://m.me/xxxx/" target="_blank"><img src="https://playground.air-srl.com/icombanner/fbico.png"/></a></li>

                    <li id="icon">
                        <a href="https://api.whatsapp.com/send?phone=03480224&text=Buongiorno, sono interessato alla vostra offerta." target="_blank">
                            <img src="https://playground.air-srl.com/icombanner/whaico.png"/>
                        </a>
                    </li>

On a "onload" JS Function, I'm forcing the CSS in this way:

document.getElementById("telephone").style="display: block";
                        document.getElementById("icon").style="display: block";
                        document.getElementById("status").style="display: block";

The result is that I've three "icon" elements as you can see, but onlythe first has this forced stye rule applied.

Any suggestion about why isn't it completely applied?

6
  • Edit: the second "li" element on HTML code has still "icon" id instead of "ico". Commented Jul 13, 2017 at 9:55
  • 1
    IDs must be unique. Commented Jul 13, 2017 at 9:56
  • id attribute intended to be unique. Browser doesn't expect you to have more than one element with id icon. Use class="icon" and document.querySelectorALl('.icon').forEach(element => element.style = "display: block;" Commented Jul 13, 2017 at 9:57
  • Thanks a lot. I've forget this rule! Commented Jul 13, 2017 at 9:57
  • @LorenzoNeri: No, even after the edit, there are two id="icon" in the above (and one id="eco"). Commented Jul 13, 2017 at 9:58

2 Answers 2

2

give your icons a class instead of id (ids must be unique in the DOM). class="icon" then

var icons = document.getElementsByClassName('icon');
Array.prototype.forEach.call(icons, function(icon){
  icon.style.display = "block";
});

document.getElementsByClassName() returns an array-like object of all child elements which have all of the given class names

Sign up to request clarification or add additional context in comments.

Comments

1

You have 3 elements with the same ID. ID is unique attribute use class instead:

<li  class="icon" ...

And change the selector in the JS function to:

(".icon")

3 Comments

There has to be a dupetarget for this.
I consider myself a Stackoverflow noob. Am I not supposed to answer semi trivial questions but look for a duplicate thread instead ?
Yes. The goal of SO is to build a database of useful programming information. Ideally, there'd be one question and set of answers which all duplicates would point to. The system falls short of that ideal, not least because it doesn't make finding duplicates very easy and doesn't reward doing so (two critical flaws in SO in my view), but that's the goal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.