How to identify which element scroll is being used using JavaScript ? Last Updated : 31 Jul, 2024 Comments Improve Suggest changes Like Article Like Report To identify which element is being scrolled using JavaScript, you can attach scroll event listeners to specific elements. This allows you to determine which element's scroll event is active, enabling tailored responses or interactions based on the particular element being scrolled.Using Scroll event listenerThis approach involves adding scroll event listeners to two scrollable elements. When either element is scrolled, the event listener updates the text content of a designated result area to indicate which element is being scrolled. This allows for real-time feedback on user interactions with the scrollable elements.Example: In this example, we've created two scrollable elements and added the 'scroll' event listener to both of them. As soon as any of the elements are scrolled, the scroll event of that particular element is fired. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>How to identify which element scroll is being used using JavaScript? </title> <style> h1 { color: #2f8d46; font-weight: bold; margin: 40px; } #first { height: 200px; width: 400px; overflow-y: scroll; margin: 40px; } #second { margin: 40px; height: 200px; width: 400px; overflow-y: scroll; } #scrolled { margin: 40px; } </style> </head> <body> <h1>GeeksforGeeks</h1> <!--First element--> <div id="first"> <h2>First element</h2> <p> JavaScript is a lightweight, cross-platform and interpreted scripting language. It is well-known for the development of web pages, many non-browser environments also use it. JavaScript can be used for Client-side developments as well as Server-side developments. JavaScript contains a standard library of objects, like Array, Date, and Math, and a core set of language elements like operators, control structures, and statements. History of JavaScript: It was created in 1995 by Brendan Eich while he was an engineer at Netscape. It was originally going to be named LiveScript but was renamed. Unlike most programming languages, the JavaScript language has no concept of input or output. It is designed to run as a scripting language in a host environment, and it is up to the host environment to provide mechanisms for communicating with the outside world. The most common host environment is the browser. </p> </div> <!--Second element--> <div id="second"> <h2>Second element</h2> <p> JavaScript is a lightweight, cross-platform and interpreted scripting language. It is well-known for the development of web pages, many non-browser environments also use it. JavaScript can be used for Client-side developments as well as Server-side developments. JavaScript contains a standard library of objects, like Array, Date, and Math, and a core set of language elements like operators, control structures, and statements. History of JavaScript: It was created in 1995 by Brendan Eich while he was an engineer at Netscape. It was originally going to be named LiveScript but was renamed. Unlike most programming languages, the JavaScript language has no concept of input or output. It is designed to run as a scripting language in a host environment, and it is up to the host environment to provide mechanisms for communicating with the outside world. The most common host environment is the browser. </p> </div> <div id="scrolled"> <h3>Element being scrolled: <span id="result"></span></h3> </div> <script> //Selecting required elements from the DOM const first = document .querySelector("#first"); const second = document .querySelector("#second"); const result = document .querySelector("#result"); //Adding the scroll event listener first .addEventListener("scroll", () => (result.textContent = "First")); second .addEventListener("scroll", () => (result.textContent = "Second")); </script> </body> </html> Output:You can see the output of the project below: Comment More infoAdvertise with us Next Article How to identify which element scroll is being used using JavaScript ? arpit2205 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Misc javaScript JavaScript-Questions +1 More Similar Reads How to Scroll to an Element Inside a Div using JavaScript? To scroll to an element within a div using JavaScript, set the parent div's scrollTop`to the target element's offsetTop. This method allows smooth navigation within a scrollable container. Below are the methods to scroll to an element inside a Div using JavaScript:Table of Content Using scrollIntoVi 3 min read How to Change Image Dynamically when User Scrolls using JavaScript ? We are going to add the functionality to our web-page so that whenever the user scrolls up or scrolls down on the image, then the image changes. We have used only 3 images but it can easily be expanded for multiple images.We are keeping the images on top of each other, this makes sure only one image 4 min read Check whether HTML element has scrollbars using JavaScript Given an HTML document, the task is to identify whether a particular element has scrollbars or not. In this article, we are going to check whether the HTML element has scrollbars using JavaScript. Below are the different approaches to check whether HTML element has scrollbars using JavaScript: Table 3 min read How to get the Width of Scroll bar using JavaScript? Given an HTML document, the task is to get the width of the scrollbar using JavaScript. Approach:Create an element (div) containing a scrollbar.OffsetWidth defines the width of an element + scrollbar width.ClientWidth defines the width of an element.So scrollbar can be defined as width = offsetWidth 3 min read How to create Infinite Scrolling using onScroll Event in JavaScript? We have seen many shopping sites that list some limited number of products(using a back-end API) when we open the website and scroll down to the bottom, we see that more products are fetched and displayed, and as we scroll even further even more products are loaded and fetched and it keeps happening 3 min read How to Determine which Element the Mouse Pointer Move Over using JavaScript? Given an HTML document and the task is to get the element where the mouse pointer moves over. Below are the approaches to Determining which Element the Mouse Pointer moves over using JavaScript: Table of ContentUsing clientX and clientY propertiesUsing onmouseover propertyApproach 1: Using clientX a 2 min read How to use Scrollspy in Bootstrap 5 via JavaScript ? In this article, we will see how to use Scrollspy in Bootstrap 5 via Javascript.Using Scrollspy in Bootstrap 5, the navigation or list group components of Bootstrap update automatically based on where the user is scrolling to indicate which link is currently active in the viewport.There are 2 ways t 6 min read How to Get and Set Scroll Position of an Element using JavaScript ? In this article, we will learn how to get and set the scroll position of an HTML element using JavaScript.Approach: We will be using the HTML DOM querySelector() and addEventListener() methods and the HTML DOM innerHTML, scrollTop and scrollLeft properties.We create an HTML div element with an id of 3 min read How to Create Scroll Indicator using HTML CSS and JavaScript ? Scroll Indicator is a progress bar that represents how much a page has been scrolled. When we scroll down the bar fills up and when we scroll up the bar amount reduces. Approach: Now, we will create a basic webpage with text to enable scrolling and then use JavaScript to make the scroll indicator wo 3 min read How to get the height of scroll bar using JavaScript ? To get the height of scroll bar we could use different approaches. In this article, we are given an HTML document and the task is to get the height of the scrollbar using JavaScript. Following are the different approaches to solving this problem which are discussed below: Table of Content Using Cont 3 min read Like