Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  159] [ 4]  / answers: 1 / hits: 34895  / 13 Years ago, wed, july 27, 2011, 12:00:00

I need to hide all elements of type 'section' in my document apart from one with a particular ID.



In jquery this would be easy



$(section).hide();
$(section#myId).show();


How would I do this without jquery??



(I need it to happen as soon as the page loads and to not be noticable). I also need it to work cross browser.



Thanks.


More From » javascript

 Answers
15

Place the following immediately before the </body> in your HTML



<script>
(function () {
for(var els = document.getElementsByTagName ('section'), i = els.length; i--;)
els[i].id !== myId && (els[i].style.display = none);
}) ();
</script>


or in modern (HTML5) browsers :



<script>
[].forEach.call (document.querySelectorAll ('section'),
function (el) { el.id !== myId && (el.style.display = none); })
</script>

[#90981] Tuesday, July 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zane

Total Points: 471
Total Questions: 94
Total Answers: 91

Location: Bahrain
Member since Sun, Mar 27, 2022
2 Years ago
;