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

Utilizing the optimal method for aligning text to either the right or left side

I am currently working on creating a container element with multiple child elements. I would like these elements to be aligned differently, some left-aligned and others right-aligned. This includes scenarios where the child elements themselves are containe ...

Using a PHP variable to dynamically change the style sheet by connecting to a MySQL database

My goal is to have the stylesheet URLs stored in a database and be able to change the stylesheets' href using variables in HTML tags. However, when I tried the following code, nothing happened: global $theme_addresss; global $showdetail_dbtadbiruse ...

Modifying the chart width in Chart.js: A step-by-step guide

After creating a chart using Chart Js, I encountered an issue where the chart did not fit within the specified width. Adjusting the attributes of canvas proved to be ineffective, specifically with regards to the width attribute. Despite changing the value, ...

Intercepting HTTP requests on specific routes with Angular 4+ using an HTTP Interceptor

I've developed an HTTP_INTERCEPTOR that needs to function on certain routes while excluding others. Initially, it was included in the main app module file. However, after removing it from there and adding it to specific modules, the interceptor conti ...

Is it possible to use Vuelidate for password validation in Vue.js?

I found a helpful reference on How to validate password with Vuelidate? validations: { user: { password: { required, containsUppercase: function(value) { return /[A-Z]/.test(value) }, containsLowercase: fu ...

Displaying the :before attribute while concealing the element solely with CSS

I have encountered a situation where I am unable to edit the HTML of a document, specifically a payment page on Shopify, for security reasons. Unfortunately, editing JavaScript is also restricted in this scenario. However, there is a workaround available ...

The vertical scrolling issue arises when the content exceeds the height of the screen

Seeking a solution to enable scrolling of users within a flexbox menu, I've come across an interesting discovery. When setting a specific height in pixels for my nav element (like 100px), everything works perfectly. However, when I set the height to 1 ...

Put the code inside a function. I'm new to this

My goal is to encapsulate this code: if($(window).width() > 980) { $(window).on("scroll", function() { if($(window).scrollTop() > 20) { //add black background $(".x-navbar").addClass("active"); $(".x-navbar .desktop ...

Saving data inputted in a form using ReactJS and JavaScript for exporting later

What is the best way to save or export form input in a ReactJS/JS powered website? For example, if I have a form and want to save or export the data in a format like CSV after the user clicks Submit, what method should I use? Appreciate any advice. Thank ...

Image Not Displayed on Page

I am facing an issue where I have a div class 'breadcam_bg' with an image url, but the image is not being displayed on the page even though the console detects the div. html <div class="bradcam_area breadcam_bg"> This div! & ...

Is there a way to transfer all the selected items to PHP using AJAX?

I am looking for a way to send all the checked items into PHP using AJAX. I want to include the checkbox ID and inner text in the information that is sent. For example, if I check box1 and box2, I want the relevant information to be saved in Data and then ...

A step-by-step guide on implementing lazy loading for a specific div section using either AJAX or Java

I'm facing an issue with a div section that contains numerous tables pulled from my database. The main problem here is that when the page loads, it takes a considerable amount of time to fully load all the content, causing a significant delay. Could ...

Tips for displaying subtotal in a Vue application using Firebase Realtime Database

I am currently troubleshooting a method in my Vue app that is designed to calculate the total value of all items sold. Despite seeing the correct values in both the database and console log, the calculation seems to be incorrect. Could there be an issue wi ...

Unable to load JQuery from a div element

My goal is to create a standard .html file containing the navigation, footer, and other elements that will be used across multiple pages for a small site I'm building. I want to keep it simple and avoid using php or other programming languages. I&apo ...

I'm looking for recommendations on database management systems that are compatible with Node.js. Can

Could you recommend a reliable database that is compatible with node.js? I am currently using node webkit and need a robust DBMS for the backend. Any suggestions on plugins that may have external dependencies would be greatly appreciated. ...

Placing elements from an array into a customized output

Currently, I am dealing with a unique output: dAmn_Raw('send chat:Sandbox\n\nmsg main\n\nthismessage'); In my code, there exists a variable: myvariable that stores a random value selected from an array I formulated. The cha ...

toggle switch for numerical input

Whenever the "Qty" radio button is selected, I need to activate an input box that accepts numbers. Conversely, when the "rate" radio button is clicked, I want to disable this input box. This is how I designed it: <input type="radio" class="radioBtn" ...

Changing the colors of multiple buttons in a React Redux form: a step-by-step guide

When using Redux Form Wizard on the second page, I have two buttons that ask for the user's gender - Male or Female. The goal is to make it so that when a user clicks on either button, only that specific button will turn orange from black text. You ...

Close the parent electron window while keeping the child window open

I am currently working on a project where I need to create an electron app that displays a splash screen initially and then opens a new window before closing the splash screen. However, despite my efforts, I am facing challenges in achieving this functio ...

Unable to display text overlay on image in AngularJS

I am experiencing an issue with displaying captions on image modals. .controller('HomeController',['dataProvider','$scope','$location', '$modal', '$log', 'authService', function ...