When I try to collapse the element at the bottom of my page, it fails to anchor to the newly opened element after clicking

I have a situation where an element at the bottom of my HTML page is not collapsed by default. When I click on an arrow just above it, the element displays. However, since the newly collapsed element is still "below the fold", users have no clue that clicking the arrow made any difference unless they scroll down.

My setup includes Bootstrap 4 and Google Chrome version 75.0.3770.90.

After some investigation, I tried setting href=idOfControl to anchor to the element, but that didn't work as expected. So it seems like I misunderstood the concept.

I also attempted using jQuery based on guidance from this jQuery scroll to element post, but had no success.

You can see a demo where clicking on a button does not automatically anchor or scroll to the newly displayed text here: https://jsfiddle.net/huntermaxfield/cnj2m86f/

This is the snippet of my actual code:

<div class="row" style="margin-top: 0px;">
        <div class="col" style="margin: 0;">
          <div class="my-5">
            <ul class="list-group list-group-flush collapse table"  id="collapseExample1">
              <li class="list-group-item px-0" style="background: beige;">
                <a class="btn collapsed collapsible-custom-sections" data-toggle="collapse">
                  <div class="spacing-in-collapse-menu">Channel</div>
                  <div class="spacing-in-collapse-menu">Actual</div>
                  <div class="spacing-in-collapse-menu">Forecasted</div>
                  <div class="spacing-in-collapse-menu">Delta</div>
                </a>
              </li>
             </ul>
            </div>
          </div>
        </div>

In summary, I am looking for a way to automatically anchor or scroll to the bottom of the newly opened element.

Answer №1

I believe the issue may be due to event.preventDefault and event.stopPropagation in the Jquery Function.

Here is a possible solution:

let buttons = document.querySelectorAll(".btn");

buttons.forEach((element) => {
element.addEventListener("click", function () {
  if (document.querySelector("#collapseExample").classList.contains("show")) {
    document.querySelector("#collapseExample").classList.remove("show");
    } else {
      document.querySelector("#collapseExample").classList.add("show");
      window.scrollTo({
        top: document.querySelector("#collapseExample").offsetTop,
        behavior: "smooth"
      })
    }
  })
})
.row {
  background: #f8f9fa;
  margin-top: 20px;
}

.col {
  border: solid 1px #6c757d;
  padding: 10px;
}

.stuff {
  height: 400px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>

<div class="stuff">

</div>

<p>
  <a class="btn btn-primary">
    Link with href
  </a>
  <button class="btn btn-primary">
    Button with data-target
  </button>
</p>
<div class="collapse" id="collapseExample">
  <div class="card card-body">
    Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
  </div>
</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

The Bootstrap 4 container class is known for leaving a peculiar dot next to the container

I am utilizing the container class from Bootstrap 4 to arrange my project details based on screen resolution sizes. However, I have encountered a strange dot that remains even after trying to remove it. You can view the HTML code I am currently working o ...

The Jquery code for Fullpage.js is causing unexpected behavior on the HTML page

While following a tutorial at the given link, I encountered an issue. Around the 9:08 mark, the instructor adds some JavaScript and demonstrates that fullpage.js is working. However, when I refresh the page after implementing the new code, it doesn't ...

Creative styling options for image thumbnails using Bootstrap

Can someone help me with an issue I'm having while using the Bootstrap Thumbnail grid system? My images are overlapping and I can't figure out why. I've attached an image for reference. Whenever I hover over the images, the ones in the midd ...

How to effectively manage an overflowing navigation bar

I am currently facing a challenge with the main navigation bar on my website, specifically with how it handles an overflow of links on smaller screen resolutions. I have attempted to adjust the height of the navigation bar at smaller media queries, but I&a ...

The height transition on a Bootstrap modal is not functioning as expected

I am currently utilizing a Bootstrap (v3) modal for the login screen on my website. Within this login screen, there is a "password forgotten" button that triggers a simple jQuery function to hide the main login form and display the forgotten password form. ...

When a mouse hovers over, introduce a smooth opacity change

I'm currently working on a project to create a React app and I'd like to incorporate an opacity fade-in transition for an image overlay when the user hovers over it. While I understand how to achieve this using the traditional ":hover" method, I& ...

Combining a left div, a right div, and a center div to equal 100% width. What is the best

I currently have a main div and three additional divs contained within it: <div id="container" style="width:100%;"> <div id="left" style="width:201px; float:left;"> .... </div> <div id="centre" st ...

A method for generating CSS code from the structure of an HTML document

I am currently working on developing a recursive function that takes an HTML document tree and generates formatted CSS using only child operators (>), but I seem to be stuck. If you could provide some hints or ideas, I would greatly appreciate it. The ...

Comparing the efficiency of Jquery draggable with thousands of elements versus updating the elements continuously

Currently, I am developing an application that involves dragging an image using Jquery's draggable utility. The image is accompanied by several overlay divs containing various components positioned based on pixel locations, sometimes reaching into the ...

How can you define rules for font fallbacks that are specific to each font?

There are times when web fonts fail to load, especially if hosted on platforms like Google Fonts. In such cases, fallback fonts may require special attention as they can vary significantly from the originally specified fonts. For example: font-family:&ap ...

Managing the placement of the expanded autocomplete input in Material-UI

Whenever I use the autocomplete fields on my website, I've observed an interesting behavior. When I select multiple options, the height of the input increases significantly, causing everything below it to shift downward like this. Is there a way to m ...

HTML: Determine the Precise Location of a Div Element That is Centered

Imagine I have this image: Is there a way for me to determine the x/y or left/top properties of my canvas if it is centered using the following CSS: #canvas-container { width: 100px; height:100px; margin: 0px auto; } Note ...

Is there a way to verify the content inside the :before selector using Jasmine within an Angular 6 application?

Looking to test whether the content of a label changes based on the checkbox being checked using Jasmine in an Angular 6 project. Is this feasible? Code snippet: HTML <input id="myCheck" class="checkbox" type="checkbox" /> <label for="myCheck" ...

reflecting on the attributes of CSS

Do you struggle with remembering CSS property names like I do? Wondering if it's common to use a cheat sheet for that purpose. If it is, could you share a reliable reference or cheat sheet for quick CSS property lookup? ...

Change the appearance of the page when it is being displayed within an iframe using CSS

How can I display a webpage differently using CSS if it is within an iframe? I would like to use jQuery or JavaScript to switch to a different stylesheet specifically when the site is being displayed within an iframe. Any suggestions on how to achieve th ...

Text positioned in a fixed location

How can I ensure that the main content stays on the right side of the sidebar without adding margin-right? Currently, the sidebar is being covered by the body content due to its fixed position. Is there a solution to prevent this issue? .App { displa ...

Alternative image loading in a figure element

I'm currently in the process of putting together an image gallery using Angular 4.3.0, where images are displayed as background images within figure elements rather than img tags. The images are initially resized to smaller dimensions before being use ...

Is there a possibility of encountering problems when collapsing table rows versus individual cells?

By using the css visibility property, you have the ability to set it to collapse for specific table rows. Nevertheless, there are two methods to accomplish this task. The first is to implement the logic on the <tr> element: thead > tr { visi ...

Tips for preventing menu flickering caused by overflow during CSS animations

I recently created a vertical menu with an interesting underline effect that appears when hovering over the menu items. Even though the snippet initially failed to execute, I discovered this cool hover effect (taken from here) which I wanted to implement: ...

The use of the picture element, with its ability to support various image formats and sizes, must always include

Recently, I came across a VueJS template that closely resembles HTML but has the following structure: <picture> <source type="image/avif" :scrset="avif" /> <source type="image/webp" :scrset="webp" ...