Search code examples
javascripthtmltransparencychildrenchild-nodes

How would I properly implement onclick using childnodes to make a div disappear?


I asked a question yesterday but it was off-topic and I have worked around the code to accomplish the goal I wanted via hover but now I want each div that is clicked to become transparent. The problem that I know have is I am working in Dreamweaver instead of phpstorm and I am not sure if the command I used is valid. My html is here:

<!DOCTYPE html>
<html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <div id="category">

            <div class="content">
            </div>

            <div class="content">
            </div>

            <div class="content">
            </div>

            <div class="content">
            </div>

            <div class="content">
            </div>

        </div>

    <style>
        div { background-color: springgreen; }
        div { width: 100px; }
        div { height: 100px; }
    </style>

</body>
<script src="js/main.js"></script>
</html>

and javascript:

/**
* Created by Mark M. on 3/28/2015.
*
*/

 var category = document.getElementById("category");
 for (var child = category.firstChild; child != null; child =    child.nextSibling) {
  if (child.nodeType == 1 && child.className == "content") {
    child.onmouseover = function() {
        this.style.width = "150px";
        this.style.height = "150px"
    };

    child.onmouseout = function() {
        this.style.width = "100px";
        this.style.height = "100px"
    };

    child.onmouseclick= function() {
        //disappear
        this.style.backgroundColor = "transparent";


    }
}}

Solution

  • Try this :

    HTML

    <div id="category">
        <div class="content"></div>
        <div class="content"></div>
        <div class="content"></div>
        <div class="content"></div>
        <div class="content"></div>
    </div>
    

    JavaScript

    var arrContent = document.querySelectorAll("#category div.content");
    for(i =0; i< arrContent.length; i++){
        var child = arrContent[i];
        console.log(child)
        if (child.nodeType == 1 ) {
            child.onmouseover = function() {
                this.style.width = "150px";
                this.style.height = "150px"
            };
    
            child.onmouseout = function() {
                this.style.width = "100px";
                this.style.height = "100px"
            };
    
            child.onclick= function() {
                this.style.backgroundColor  = "transparent";
            }
        }
    }`
    

    StyleSheet

    div.content {background-color: springgreen;width: 100px;height: 100px;}
    

    var arrContent = document.querySelectorAll("#category div.content");
    for(i =0; i< arrContent.length; i++){
    var child = arrContent[i];
    console.log(child)
    if (child.nodeType == 1 ) {
        child.onmouseover = function() {
            this.style.width = "150px";
            this.style.height = "150px"
        };
    
        child.onmouseout = function() {
            this.style.width = "100px";
            this.style.height = "100px"
        };
    
        child.onclick= function() {
            this.style.backgroundColor  = "transparent";
        }
    }
    }
    div.content {background-color: springgreen;width: 100px;height: 100px;}
    <div id="category">
    <div class="content"></div>
    <div class="content"></div>
    <div class="content"></div>
    <div class="content"></div>
    <div class="content"></div>
    </div>