Skip to content Skip to sidebar Skip to footer

How To Get Dom Elements In A Particular Element?

What I want to do I want to get the center of the height of a specific element .main and get the DOM element that is there. What I tried I thought I could get a DOM element wi

Solution 1:

You are getting height and width centers but not adding the offsets for main from left and top of the document

I found element p.active by adjusting your demo to:

const rect = document.getElementsByClassName("main")[0].getBoundingClientRect();

const mainHCenter = rect.top + (rect.height / 2);
const mainWCenter = rect.left + (rect.width / 2);
                  // ^^^^^   add left and top offsetsvar centerElm = document.elementFromPoint(mainWCenter, mainHCenter);
                                        // ^^^ don't use zeroconsole.log(centerElm);// returned p.active

DEMO

Post a Comment for "How To Get Dom Elements In A Particular Element?"