Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Can someone clarify why I am getting two instead of one when I clicked the div because 2 < 2 can't be valid?

<!DOCTYPE html> <html> <body> <div>0 <script> let i = 0; let div = document.querySelector('div') div.addEventListener('click',function(){ if(i < 2){ i += 1 div.innerHTML = i } }) </script> </body> </html>

2 Answers

I reformatted you code below, and added semi colons as necessary, now;

On the first click, 0 < 2 is true so i becomes one. On the second click 1 < 2 is true so i becomes two. On the third click 2 <2 is false so nothing happens

<!DOCTYPE html>
<html>
<body>
  <div>0
    <script>
      let i = 0;
      let div = document.querySelector('div');
      div.addEventListener('click',
        function () {
          if (i < 2) {
            i += 1;
            div.innerHTML = i;
          }
        });
    </script>
</body>
</html>

I trying to apply a ripple effect on child of a div, but I don't how to make the animation based on where I clicked on.

<!DOCTYPE html> <html> <head> <title>Page Title</title> <style> body > div{position:relative; height:100%;overflow:hidden; width:100%;border:3px solid #000; } @keyframes move{ 0%{ transform:scale(0);opacity:1; } 100%{ opacity:0;transform:scale(1); } } .child{ height: 100%; opacity:0; cursor:pointer; background: rgba(0, 0, 0, 1); width: 100%; position: absolute; }

.blue{animation:2.5s move; }

,::before,*::after{margin:0;padding:0;box-sizing:border-box;}

</style> </head> <body> <div tabindex="0"> <div class="child"></div> </div> <script> const parent = document.querySelector('div') const child = parent.children[0] child.addEventListener('click',function(e){

child.classList.add('blue') child.style.left = e.clientX + "px"; child.style.top = e.clientY + "px";

}) </script> </body> </html>