Navigating the page by utilizing Javascript through a CSS selector

I am encountering a small issue with scrolling a pop-up window. I am trying to scroll a popup that contains a div and some CSS code without using an ID or class.

Here is the HTML CSS Code snippet:

<div style="height: 356px; overflow: hidden auto;">

Below is the JavaScript code I have attempted:

var objDiv = document.getElementsByTagName("div");
objDiv[0].scrollTop = objDiv[0].scrollHeight;

The same code works for classes and IDs as well:

<div class="123">

JavaScript code for classes:

var objDiv = document.getElementsByClass("123");
objDiv[0].scrollTop = objDiv[0].scrollHeight;

This relates to an Instagram-like pop-up. I am seeking a way to scroll using either a div, a CSS selector, or both simultaneously with JavaScript code.

Answer №1

Your code may need adjustments if there are multiple divs on the page. The first div might not necessarily be the one you want to target.

If you wish to scroll through all overflow divs on the page, you can use the following script:

var divs = document.getElementsByTagName("div");

for (let div of divs) {
  if (div.style.overflow.includes("auto")) {
    div.scrollTop = div.scrollHeight;
    // Uncomment the break statement if you only need the first match
    // break;
  }
}

If you prefer to specify a particular parent element (like your popup) and then select the child div using querySelector, you can try something like this:

For example:

document.querySelector("#popup_div_id div");

Solution adapted based on your feedback

var div = document.querySelector("div[style='flex-direction: column;']");
div.scrollTop = div.scrollHeight;
div {
  height: 100px;
  width: 200px;
  overflow: auto;
}
<div style="flex-direction: column;">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras in viverra justo. Praesent in eros eget dui accumsan accumsan et suscipit ante. Aenean nec eros dignissim, luctus magna quis, rutrum augue.
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Google App Script's pageToken to store attachments on Google Drive

Essentially, my goal is to save all attachments from received emails to a specific folder in Google Drive (mostly .PDF files). However, I've encountered a limitation with the search function which only allows me to retrieve up to 500 attached files. I ...

Angular not executing Jquery script in included HTML files

Underneath is a simple jquery code snippet: $(document).ready(function() { $("h2").click(function(){ alert("Hello world"); }) }); After clicking on any h2 tag in the website, an alert message will display. The code functions properl ...

How come my MySQL date is decreasing by one day when using JavaScript?

My todo list is stored in a MySQL database with columns for todoTitle and todoDate. However, when I display the todoDate on my website, it shows the date decremented by one day. For example, if the date in the database is 2016-12-20, it will show as 2016-1 ...

What is the best way to show a message on a webpage after a user inputs a value into a textbox using

I have a JSFiddle with some code. There is a textbox in a table and I want to check if the user inserts 3000, then display a message saying "Yes, you are correct." Here is my jQuery code: $("#text10").keyup(function(){ $("#text10").blur(); ...

What is the method for obtaining the angle in a CSS3 rotate transformation?

In my current code, I am setting the rotational angle of an element using the following: $('#1-link-2') .css('height', length) .css('-webkit-transform', 'rotate(' + angle + 'deg) ...

Incorporating a Streamlit Application into an Established React.js Project via an iFrame

I am in the process of incorporating a Streamlit app into my existing React.js application to take advantage of its data analysis and visualization features. Can this integration be accomplished using an iFrame? Has anyone successfully embedded a Streamlit ...

Library for creating custom password input fields with a text type

We are currently facing an issue on our website where autofill is not working correctly due to having two password input fields - one for login and one for the password. Browsers are unable to remember the login information and paste the password into th ...

My attempt to incorporate an ajax loading gif into my website has been met with technical difficulties and is currently not functioning as

I am currently developing an uploader feature for my website located at To enhance user experience, I aim to display a loading animation in the form of a .gif image while the select box is being populated with subcategory options via an AJAX request. Bel ...

Selenium navigating an unordered list without returning the expected text

I'm currently in the process of developing a search bot for Craigslist using Selenium. I've managed to successfully iterate through the unordered list of search results, but unfortunately, I'm not able to extract the specific link text that ...

Error: The Mui Material Breakpoints Theme Cannot Be Located

Hello there! I'm having some trouble placing my code breakpoints, resulting in an error in the output. Can you help me? import { makeStyles } from "@material-ui/styles"; const useStyle = makeStyles((theme)=>({ LogoLg:{ display:&ap ...

Expanding the functionality of Promise to include progress updates

I had the idea to expand Promise by adding a 'progress' feature in order to track progress while using Promise for my asynchronous tasks. So, I created an extended version of Promise like this: class promisePro extends Promise { constructor ...

Creating a responsive database using an Express application and Socket.IO

As I am developing an application using Express Create App, specifically designed to run on Plesk hosting, I have already configured it following the example provided by the official Plesk Node Express project. Everything is working smoothly so far. The ap ...

Activating the submit button only following confirmation that the image dimensions have been verified

I've written some code that checks whether selected pictures meet specific size and dimension constraints before enabling the submit button. However, there's an issue where the button might be enabled before verifying the last image due to delays ...

A guide on designing a personalized search bar for MUI-Datatables with a sleek outlined style

Check out the default UI from MUI-Datatables v4.3.0 here: https://i.stack.imgur.com/rbHgD.png I want to create a style similar to this: https://i.stack.imgur.com/AUHqC.png Just so you know, I am using the following packages: "@mui/material": &q ...

React is still unable to display images when looping, despite the use of the "require"

I am facing an issue with extracting image paths from an array using the map function. The problem is that the image path is being printed as a string instead of rendering as an image. I tried using the require keyword, but I encountered an error stating " ...

Utilizing doT.js for Organizing Nested Lists from Arrays and Objects

Can nested lists be generated with doT.js? My current code only processes the first object in the array (g1) and ignores the rest. Is there a solution for this using doT.js? The desired result should be: G1 T11 T12 T13 G2 T21 T22 T23 $(document) ...

sudden swivel when shifting camera while following an object on a flat surface/pathway

I'm relatively new to three.js and I'm eager to create a scenario where a cube or object moves on a flat surface or road. I have managed to move the object in the z direction, but now I want to move the camera as well, giving the impression of a ...

A guide on utilizing the javascript function to toggle the checkbox in each row of a gridview

I am looking to implement a function that checks checkboxes row by row based on specific conditions. The first condition requires an alert popup if three checkboxes are selected in the same row consecutively ("You can't select continuous three hou ...

React Braces Dilemma

I'm a bit embarrassed to admit it, but I'm struggling with writing something simple in React using JSX. Despite searching online and consulting various resources, including the official React documentation, I am still unable to grasp the concept. ...

Having trouble with my jQuery code not functioning as expected

I'm currently a first-year student in an ICT program and we're working on a project. Right now, I'm focused on writing jQuery code for a shopping cart feature. My goal is to input the quantity of items I want to add (e.g. 5) and have the to ...