jQuery appears to be unresponsive or inactive

I'm trying to implement a jQuery script that will slide in a header after scrolling on the page, but for some reason, it's not working. When I reach the 3rd line, my code editor displays a !read only alert, suggesting there may be a syntax issue?

Just so you know, I am using Beaver Builder, which automatically includes jQuery.

$( document ).ready(function() {
        $(window).scroll(function(){
            scroll = $(window).scrollTop();
            if (scroll > 450){
                $('#jQuery-mob').slideDown();
            }

            if (scroll < 450){
                $('#jQuery-mob').slideUp();
            }

        });
    });

        $( document ).ready(function() {
        $(window).scroll(function(){
            scroll = $(window).scrollTop();
            if (scroll > 700){
                $('#why-jquery').slideDown();
            }

            if (scroll < 700){
                $('#why-jquery').slideUp();
            }

        });
    });

For both #jquery-mob and #why-jquery elements, their initial display is set to none.

CSS:

#why-jquery {
    position: fixed;
    top:0;
    z-index: 99;
    width: 100%;
    height: 100px;
    display: none;
}

#jQuery-mob {
    position: fixed;
    top:0;
    z-index: 99;
    width: 100%;
    height: 100px;
    padding: 0 !important;
    display: none;
}

Answer №1

I typically utilize beaver builder, which automatically incorporates jQuery into my projects.

However, it seems like you may have confused that with:

I work with wordpress sites, where jQuery is called automatically.

For wordpress websites, the default practice is to use jQuery instead of $. You can either replace all instances, or solely employ jQuery within the .ready "wrapper" and pass the $ into the .ready function.

Here's an example of both approaches:

jQuery( document ).ready(function() {
    jQuery(window).scroll(function(){
        scroll = jQuery(window).scrollTop();
        if (scroll > 450){
            jQuery('#jQuery-mob').slideDown();
        }

        if (scroll < 450){
            jQuery('#jQuery-mob').slideUp();
        }
    });
});

jQuery(document).ready(function(  $  ) {
    $(window).scroll(function(){
        scroll = $(window).scrollTop();
        if (scroll > 700){
            $('#why-jquery').slideDown();
        }

        if (scroll < 700){
            $('#why-jquery').slideUp();
        }
    });
});

Please note that I haven't verified your code, but I noticed you're using scroll without declaring it with var. This implies that scroll will be a global variable. It could potentially lead to conflicts between functions and even overwrite the window.scroll function. To avoid this, consider using var scroll= and opt for a different variable name.

Answer №2

scroll functions at the window level in a browser, making it a reserved word:

console.log(scroll)

If you attempt to change it with a variable named "scroll", that's what triggers the "!read only" error mentioned.

To resolve this, use a different variable name and declare it using 'var'.

Answer №3

This code is facing a logical problem

  if (when scrolled less than 450 && also less than 800){ 

It is redundant to have the second comparison when checking if scroll is smaller than 450, as it will always be smaller than 800 in that case.

A similar issue arises on this line:

if (when scroll is under 700 && but less than 1000){

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

What is the most efficient way to prevent duplicate items from being added to an array in a Vue 3 shopping cart

I currently have a functional shopping cart system, but I am facing an issue where it creates duplicates in the cart instead of incrementing the quantity. How can I modify it to only increment the item if it already exists in the cart? Also, I would like t ...

Displaying block in front of flex-items

I have been trying to figure out how to get the red box to display in front of the other flex items, but none of my attempts seem to work. .box { font-size: 2.5rem; width: 100px; line-height: 100px; text-align: center; } .front .box { margin: ...

The svg line is drawn with a width that is half of the specified width

I am looking to create a horizontal line that is 10px wide. I tried using the code below <svg width="500" > <line x1="100" x2="460" y1="0" y2="0" stroke="red" stroke-width="10px&qu ...

Detecting Collisions in a Canvas-Based Game

As part of my educational project, I am developing a basic shooter game using HTML5 canvas. The objective of the game is to move left and right while shooting with the spacebar key. When the bullets are fired, they travel upwards, and the enemy moves downw ...

CSS dilemma: A snag with pointer-events and the a:focus attribute

Does anyone have experience troubleshooting issues with navbars? I could really use some help. I am facing an issue with my navigation bar where I have different links that reveal an unordered list of sublinks upon clicking. The CSS for my <ul> is ...

What methods can I use to identify the selector for this element?

After pinpointing the specific element in Google Chrome using inspect element, what kind of path or syntax should I use to apply a method to this element? The hierarchy for the element is outlined below: html body div#contentDiv div#searchFormDiv ...

Executing a function when a webpage loads in Javascript

I have created a responsive website using Twitter Bootstrap. On my site, I have a video displayed in a modal that automatically plays when a button is clicked. Instead of triggering the function with a button click, I want the function to be called when ...

Securing NextJS API Routes with API Key Authorization

I'm developing an app in NextJS 13 that utilizes API routes, and I aim to secure them against any unauthorized access from external functions. While my app integrates Auth0, there is no strict requirement for protection since unregistered individuals ...

Organize the dataset into groups based on every possible key

Hi there, I'm facing a challenge while developing an application using NestJS/Prisma. The task at hand is to extract unique results from a table in order to display them in a filter on the front-end. Let me give you an overview of my table structure ...

What could be causing my selenium tests to fail on travis-ci even though there have been no code changes, when they are passing successfully

I'm facing a tough challenge trying to troubleshoot a selenium test that passes when run locally but not on travis. Reviewing the travis build logs, I noticed that the test was passing in build #311 but started failing at build #312. It seems like th ...

recording the results of a Node.js program in PHP using exec

I'm attempting to retrieve the output from my node.js script using PHP exec wrapped within an ajax call. I am able to make the call and receive some feedback, but I can't seem to capture the console.log output in the variable. This is how I exec ...

The fitBounds and panToBounds functions in react-google-maps fail to properly adjust the map's size

const Map = withGoogleMap(props => { const { activeMarker, zoom, center, showInfoWindow, products } = props; const [selectedPlace, setSelectedPlace] = useState(null); // Code to iterate through items and adjust map size, center, and zoom to inclu ...

The code functions properly when tested on a local server, but doesn't produce any results when

Currently, I am working on customizing a premade website and have been tasked with making a modification. The goal is to implement a confirmation box that appears when a button to delete a meeting task is clicked, rather than immediately deleting it. Afte ...

Firestore is failing to accept incoming data when the setDoc method is employed

I am encountering an issue with my app's connectivity to the Firestore database when attempting to utilize setDoc. The database is successfully operational for next-auth, creating records as intended. However, the problem arises when trying to enable ...

Unable to integrate bower with gulp for automation

I am trying to get gulp to run the installation of all files listed in my bower.json. However, despite writing the code below, it is not working and I'm not seeing any errors. What could be causing this issue? var gulp = require("gulp"); var bower = ...

Configuring Jest unit testing with Quasar-Framework version 0.15

Previously, my Jest tests were functioning properly with Quasar version 0.14. Currently, some simple tests and all snapshot-tests are passing but I am encountering issues with certain tests, resulting in the following errors: console.error node_modules/vu ...

Accessing Parent and Child Values in AngularJS Selections

I am seeking advice from experts on how to achieve the following desired results: Expected workflow chart: Here is the default view: Scenario 1: By clicking on the number "1", all items from left to right and their children should be highlighted as show ...

Creating content in HTML that features two div elements centered vertically on a page, with one div aligned to the left and the other aligned to the right

Implementing Bootstrap for styling my HTML page has resulted in the following layout: https://i.sstatic.net/wzsnc.png Currently, both words are positioned at the top of the screen. However, I aim to have them vertically centered. Additionally, they are c ...

Creating a transparent background in a three.js canvas: a step-by-step guide

I came across a wave particle animation on Codepen (https://codepen.io/kevinsturf/pen/ExLdPZ) that I want to use, but it has a white background. However, when I try to set the body background to red, it doesn't show up once the canvas is rendered. I ...

Customize your WooCommerce Cart page by adding fees for selected options

I have recently updated my cart page to include checkboxes that allow customers to add extra items to their orders. These changes were inspired by an answer on Custom checkbox that adds a fee in WooCommerce cart page. The last checkbox successfully doubl ...