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 JavaScript and the DOM (Retiring) Getting a Handle on the DOM Selecting Multiple Elements

I've used getElementById('rainbow').getElementsByTagName('li') and it worked. Why?

I've successfully used:

getElementById('rainbow').getElementsByTagName('li')

But I thought it shouldn't work.

I thought .getElement or .getElements is a DOM method and therefore should be applicable to the document object only. Or am I wrong?

DOM methods are applicable to HTML Elements, or more accurately, objects within the DOM.

In your case, document.getElementById('rainbow') is an object with the ID of rainbow. Since it is an object, the .getElementsByTagName() method still works on it. So, document.getElementById('rainbow').getElementsByTagName('li') will successfully return an array of the <li> elements under #rainbow.

However, it is usually best practice to assign #rainbow to a variable and use .getElementsByTagName() on the variable.

1 Answer

Let's rewrite this to make it easier to follow::

let ulElement = document.getElementById('rainbow');
let liElementList = ulElement.getElementsByTagName('li') ;

The first call is looking for a ul element within the document. The second is looking for a list of li elements within the ul element. So the calls are not all based off the document. It's referred to as object chaining when the code is all together:

getElementById('rainbow').getElementsByTagName('li'); The first call returns the ulElement, the second call is based object returned from the first call.

Please someone help me with JavaScript to apply a ripple effect .

``

<!DOCTYPE html> <html> <head> <title>Page Title</title> <style> body > div{position:relative; height:256px; overflow:hidden; width:256px; border:3px solid #000; }

@keyframes move{ 0%{ transform:scale(0);opacity:1; } 100%{ opacity:0;transform:scale(21); } }

.red{ height: 100%; opacity:0; cursor:pointer; background: rgba(0, 0, 0, 1); width: 100%; pointer-events:none; 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="red"></div> </div> <script> const parent = document.querySelector('div'); const child = parent.children[0] parent.addEventListener('click',function(e){

if(e.target.tagName === 'DIV'){ child.classList.add('blue'); child.style.left = e.clientX + "px"; child.style.top = e.clientY + "px"; }

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

``