Please tell me , is the task using JavaScript, find all the links with the class .button and perform an action on them (calling the modal window). querySelectorAll does not help , since classes seem to be in different DOM branches. How to be? Do you have any hacks or something that I really misunderstand?

html for example:

<header> <a href="" class="button"></a> </header> <main> <a href="" class="button"></a> <a href="" class="button"></a> <a href="" class="button"></a> </main> <footer> <a href="" class="button"></a> </footer> 

Js

 var link=document.querySelector(".button"); var popup = document.querySelector(".modal-window"); var close = document.querySelector(".close-form"); link.addEventListener("click", function(event) { event.preventDefault(); popup.classList.add("modal-window-see"); }, false); close.addEventListener("click", function(event) { event.preventDefault(); popup.classList.remove("modal-window-see"); }, false); 
  • > classes appear to be in different DOM branches. I'm sorry, what? - etki

1 answer 1

  1. judging by the code you did not add a piece of html here
  2. Call js code after building a DOM tree or enclose it in functions like window.onload or $ (document) .ready ()
  3. Here's a little bit of the code http://jsfiddle.net/Hr5Cn/1/
  • Thanks, mountpoint! I still don’t know much about js, so I hardly digest even that. How points will be enough I will put + - gershmit