# W3schools modal not working

**URL:** <https://forum.freecodecamp.org/t/w3schools-modal-not-working/418008>\
**Category:** General\
**Created:** [August 30, 2020, 7:02am UTC](https://forum.freecodecamp.org/t/w3schools-modal-not-working/418008 "2020-08-30T07:02:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vicrost](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vicrost/32/154609_2.png) [@vicrost](https://forum.freecodecamp.org/u/vicrost)\
**Post date:** [August 30, 2020, 7:02am UTC](https://forum.freecodecamp.org/t/w3schools-modal-not-working/418008/1 "2020-08-30T07:02:53Z")

</div>

I followed the w3schools tutorial for creating a modal. However when i click on the button, nothing happens. The only change i made to the tutorial was i used getElementbyClassName instead of id.  
This is the tutorial link: [https://www.w3schools.com/howto/howto\_css\_modals.asp](https://www.w3schools.com/howto/howto_css_modals.asp)

this is my modal code:

```auto
<button class="modal-btn"> Click me</button>

         <div class="modal-container">
 
            <div class="modal-content">

                <span class="close">&times;</span>

                <p> Some text in modal</p>

            </div>

        </div>

```

and this is my js file (I added everything in case one might be interfering)

```auto

$(window).on('scroll',function(){

  if ($(window).scrollTop()){

    $('nav').addClass('black');

  }

  else{

    $('nav').removeClass('black');

  }

})

$(document).ready(function(){

  $(".menu h4").click(function(){

    $("nav ul").toggleClass("active")

  })

})
> 
  //modal 

var modal = document.getElementsByClassName('modal-container');

 // Get the button that opens the modal

var btn = document.getElementsByClassName("modal-btn");

 // Get the <span> element that closes the modal

var span = document.getElementsByClassName("close");

// When the user clicks the button, open the modal 

 btn.onclick = function() {

modal[0].style.display = "block";

}

// When the user clicks on <span> (x), close the modal

span.onclick = function() {
 
modal.style.display = "none";

}

// When the user clicks anywhere outside of the modal, close it

window.onclick = function(event) {

if (event.target == modal) {

    modal.style.display = "none";
 }

}

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [August 30, 2020, 8:58am UTC](https://forum.freecodecamp.org/t/w3schools-modal-not-working/418008/2 "2020-08-30T08:58:20Z")

</div>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](http://www.freecodecamp.com/forum/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![akshayparmar90](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/akshayparmar90/32/135833_2.png) [@akshayparmar90](https://forum.freecodecamp.org/u/akshayparmar90)\
**Post date:** [August 31, 2020, 6:13pm UTC](https://forum.freecodecamp.org/t/w3schools-modal-not-working/418008/3 "2020-08-31T18:13:05Z")

</div>

> [@vicrost](#):
>
> `var btn = document.getElementsByClassName("modal-btn");`

Insted of using document.getElementsByClassName use document.getElementById…  
because here [W3Schools Tryit Editor](https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal)

if you edit as a getElementsByClassName and run It will not work.

---

<div class="post-metadata">

**Author:** ![vicrost](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vicrost/32/154609_2.png) [@vicrost](https://forum.freecodecamp.org/u/vicrost)\
**Post date:** [September 1, 2020, 5:50am UTC](https://forum.freecodecamp.org/t/w3schools-modal-not-working/418008/4 "2020-09-01T05:50:53Z")

</div>

I asked the question on stackoverflow and I found out that the problem seems to be something with my pc. When I run the code on codepen, it works perfectly fine but on VS Code, nothing works. I’ve followed two other modal tutorials and they all don’t work. Do you have any suggestions.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:41pm UTC](https://forum.freecodecamp.org/t/w3schools-modal-not-working/418008/5 "2021-06-01T18:41:39Z")

</div>

Closed on June 1, 2021, 6:41pm UTC


