Display or conceal buttons based on the overflow height of another div

I want to dynamically show or hide buttons based on the overflow of a navigation list. If the list height overflows, display the buttons; if not, hide them.

Using JQuery

 // This code snippet did not work for me! 
    if($('#nav-list').prop('scrollHeight') < $('#nav-list').height()){
    $('#updown-arrows').hide();
    }else{
    $('#updown-arrows').show();
    }

HTML Structure

<div id="nav-list">
 
  <!-- Wordpress navigation menu -->
  <?php
  wp_nav_menu(
    array(
      'theme_location'    => 'primary',
      'depth'             => 2,
      'menu_class'        => 'list-unstyled components',
      'menu_id'         => 'sidebarnav',
      'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',
      'walker'            => new wp_bootstrap_navwalker()
    )
  );
  ?>
</div>
<!-- Buttons for scrolling up and down -->
<div id="updown-arrows">
  <a  id="up" href="#"><span class="glyphicon glyphicon-menu-up" aria-hidden="true"></span></a>
  <a  id="down" href="#"><span class="glyphicon glyphicon-menu-down bounce" aria-hidden="true"></span></a>
</div>

Answer №1

Has anyone ever encountered a similar problem?

  function checkOverflow() {
            if ($('#nav-list').prop('scrollHeight') <= $('#nav-list').prop('clientHeight')){
                    $('#updown-arrows').hide();
                    
                } else {
                    $('#updown-arrows').show();
                    
                }
            }

            checkOverflow();
            $(window).resize(function () {
                checkOverflow()
            })

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

Executing Multiple AJAX Requests Sequentially with JQuery

Is there a way to ensure that ajax calls in jQuery are executed in sequence without using async: false? I have tried utilizing ajaxComplete but it has not resolved the issue. The code provided loops through elements in an array and fires off ajax request ...

Recursive rendering of tree components in React

I am facing a challenge in rendering tree items recursively using React. I have been struggling to achieve the desired outcome as calling treeRender(children) seems to alter the data structure when a folder is encountered for the first time. I am curious a ...

Even though I am aware that the variable AJAX is attempting to return is not empty, it is still returning 'undefined'

I wrote a basic PHP script that retrieves information from a database and stores it in a multidimensional array: <?php //PHP code to fetch data from DB error_reporting(E_ALL); $db = new mysqli("localhost","root","pass", "Media") ...

Issue with MongoDB: Using $unset with an empty parameter results in a 409 error

When updating values dynamically, I often need to set or unset them. However, there are times when I only need to do one or the other. Here is my current approach: Collection.update( { _id: id }, { $set: data, $unset: remove ...

Error: DataTables FixedColumn module "Uncaught ReferenceError: FixedColumns is not defined"

I am currently struggling to implement the FixedColumns plugin from datatables. I am following the example code provided on the website: $(document).ready( function () { var oTable = $('#example').dataTable( { "sScrollX": "100%", ...

Divide the sentence using unique symbols to break it into individual words, while also maintaining

Is there a way to split a sentence with special characters into words while keeping the spaces? For example: "la sílaba tónica es la penúltima".split(...regex...) to: ["la ", "sílaba ", "tónica ", "es ", "la ", "penúltima"] ↑ ...

The total sum of values within labels that share the same class

I am attempting to sum up all the values of class tmpcpa and store the result in final_cpa, but for some reason final_cpa always ends up being 0. document.getElementById('cpa' + arr[0]).innerHTML = cpa + '(' + '<label id="tmp ...

Scroll through content using buttons in JavaScript can be utilized repeatedly as needed

I want to showcase a collection of movies on my website through a horizontal, scrollable div. Since I disabled the scrollbar, users can no longer scroll, leading me to incorporate two buttons - one for moving right and one for moving left. However, the i ...

Leverage PHP to retrieve the value from a jQuery div element

One of my <div id="single-home-container"></div> is showing a value generated by a jQuery script. Is there a way to utilize PHP in order to retrieve that value for the purpose of passing it into a MySQL query? Thank you. ...

The AJAX success callback is receiving a JSON response but unable to parse it

I have implemented the AJAX function below. Although I am able to retrieve the correct response headers within the success function of AJAX, I am encountering an issue where the parsed response returns as undefined. The JSON data structure that is being r ...

Accessing JSON files within a subprocess

I have encountered a situation where I am working with two interconnected Node.js apps. One app is calling another using the child process exec method. Within one of the apps, I am attempting to read JSON files using the following method: function readAs ...

Utilize Modal popup for ADDing, EDITing, and DELETing functionality in ASP.net MVC

After spending a significant amount of time trying to understand how to effectively use modal popups, I have yet to find a satisfactory answer or a full working code. I am seeking an approach that utilizes MVC HTTPGET and HTTPPOST methods, without hardcod ...

"Ng-repeat function seems to be malfunctioning, although I am able to view

There seems to be an issue with ng-repeat when dealing with an array of objects: dummy.json [ {"name":"alpha", "data":[{"name":"Unit 1", "prereqs":[]}]}, {"name":"beta", "data":[{"name":"Unit 1", "prereqs":[]}]} ] While I am able to fetch the total ...

Steps to invoke another service (function) prior to calling the res.view() function in sails JS:

I am looking to execute a separate function, like a Service function, before Sails renders the page into HTML. Please refer to the controller code below... var MYController = { index: function(req, res, next) { req.flash("error", "Testing hello ...

Can we import a CSS file selectively in Vue.js?

Can specific css-files be applied only when the current route has a "forAdmin" meta in my Vue.js project that includes both an admin-panel and client pages? ...

Create a universal function similar to onbeforeunload that works seamlessly across all web browsers

Can I implement an onbeforeunload function that works on all browsers? window.onbeforeunload = function () {/**/} I need to run a script when a link on my website is clicked. Currently, I'm using an onclick event-based script, but it doesn't f ...

The pagination functionality in the customized React Native list component is malfunctioning

In my customized list component known as TableList, there is a pagination functionality implemented. However, a peculiar behavior occurs when the user interacts with the pagination arrows. Upon clicking either the next or previous arrow for the first time ...

Changing color of entire SVG image: a step-by-step guide

Check out this SVG image I found: https://jsfiddle.net/hey0qvgk/3/ <?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 19.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <svg version="1.1" width="90" height="9 ...

Issue with retrieving data using AngularJS Restangular

I've been trying to figure out how to make restangular work properly. When I call my API (using the endpoint /user) I receive the following JSON response: { "error": false, "response": { "totalcount": 2, "records": [{ "id": "1", ...

"Enhancing User Experience with Bootstrap's masonry layout feature while retaining scroll position

I'm faced with a puzzling situation and I can't seem to find the solution. Currently, I am utilizing Bootstrap 5 along with the masonry layout as recommended here: https://getbootstrap.com/docs/5.0/examples/masonry/, and it is working perfectly. ...