0

I just want to set display property of one of my div to block based on event, by default its 'none' Its not happening with error: Cannot set property 'display' of undefined Below is my code. Please suggest.

HTML

<!DOCTYPE html>
<html >
<head>
<meta charset="ISO-8859-1">
<title>Sample Mod Pop UP</title>
<link rel="stylesheet" href="css/main.css" />
</head>
<body class="body">


<div class="popUp" id="id1">
  <p>Are You Sure You Want to Quit?</p>
  <span>&#10006</span> 
</div>
<button onClick=closeWindown() class="btn">Close</button>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>

JavaScript

function closeWindown(){

    document.getElementsByClassName("popUp").style.display="block";


}
0

1 Answer 1

4

getElementsByClassName gets a nodeList, so you have to iterate

function closeWindown(){

    var elements = document.getElementsByClassName("popUp");

    for (var i=elements.length; i--;) {
        elements[i].style.display = "block";
    }

}

As a sidenote, querySelectorAll('.popup') has better support, but is non-live.

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

Comments

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.