0

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";


    }
}}
Rafa Viotti
  • 9,998
  • 4
  • 42
  • 62
Mark Gregg
  • 83
  • 1
  • 2
  • 10
  • Can you make fiddle for this ? – TechnoCrat Apr 02 '15 at 02:29
  • pure native JS or jquery ok? – Sai Apr 02 '15 at 02:31
  • 1
    The markup is invalid, script elements must be within the head or body. If you want to process just the *div* elements that are children of *category*, likely a selector is a better way to go. – RobG Apr 02 '15 at 02:38
  • If you are literally trying to make the div disappear without affecting layout, then `this.style.visibility = 'hidden'` will do the job. Or if you want them to not take up any room at all, use `this.style.display = 'none'`. But it would be better to apply both by adding a class value and removing the effect by removing the class. – RobG Apr 02 '15 at 02:48
  • i would like to use pure native js sorry for the late response. – Mark Gregg Apr 02 '15 at 02:55
  • I don't really mind it affecting layout i just want the div to not be visible after a click. And I am currently trying to implement some of the things people have written here but there is a lot of information. – Mark Gregg Apr 02 '15 at 02:56

3 Answers3

1

This should be an elegant solution for your question. Click "Run Code Snippet" to check:

var contentDivs = document.getElementsByClassName('content');

for(var i = 0; i < contentDivs.length; i++) {
    var div = contentDivs[i];
    div.onclick = function(){
        this.style.display = 'none';
    }
}
div {background-color: springgreen; width: 100px; height: 100px;}
<div id="category">
    <div class="content">Content 1</div>
    <div class="content">Content 2</div>
    <div class="content">Content 3</div>
    <div class="content">Content 4</div>
    <div class="content">Content 5</div>
</div>
Huy Hoang Pham
  • 4,107
  • 1
  • 16
  • 28
0

The problem is that you are using 'onmouseclick' instead of 'onclick'

how about using document.getElementsByClassName('content'), and addEventListener() instead of using the 'onX' syntax. I believe the addEventListener is more standards compliant and should provide better browser compatibility.

https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

var content = document.getElementsByClassName('content');
console.log('content found', content);
for (var i = 0; i < content.length; i+=1){
  content[i].addEventListener("click", function(){
    this.style.backgroundColor = "transparent";
  });
}
Allan Nienhuis
  • 3,961
  • 2
  • 23
  • 19
  • 1
    Or maybe `var content = document.querySelectorAll('#category > div')`. ;-) – RobG Apr 02 '15 at 02:45
  • yes, I was just about to add the querySelectorAll suggestion. You beat me to it :) https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll – Allan Nienhuis Apr 02 '15 at 02:47
  • 1
    or may be `var content = document.querySelectorAll('#category > div.content')` :P – super Apr 02 '15 at 02:47
  • I'd probably use event delegation and put a single listener on the *content*. – RobG Apr 02 '15 at 02:53
  • I would agree with using event delegation http://davidwalsh.name/event-delegate if we're talking about really, really long lists of .content items. For most cases I doubt it would ever make any practical difference :) – Allan Nienhuis Apr 02 '15 at 03:01
  • That said, if he is adding and removing .content items with ajax, event delegation is absolutely the right approach. While jquery is overused in many places, it has a really nice api for that which makes it a lot easier to manage the selectors. – Allan Nienhuis Apr 02 '15 at 03:05
0

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>
super
  • 2,288
  • 2
  • 21
  • 23
  • this achieved the desired effect. I have been trying to comment on everyone's posts but I didn't expect such a large amount of help and I do appreciate it and try out each different way that people post it. – Mark Gregg Apr 02 '15 at 03:26