Using the jQuery each loop with Waypoints

Having trouble with the $.each() function in Waypoints and could use some assistance. I've checked other Stack Overflow posts using the same method but still can't seem to get it working properly.

You can find my code on JSFiddle: https://jsfiddle.net/rs80dmn5/5/

Below is the HTML:

<ul class="col-sm-6">
                <li>
                    <h2>10<span>year</span></h2>
                    <h2>100,000<span>miles</span></h2>
                    <p>Powertrain<br/>Warranty</p>
                </li>
                <li>
                    <h2>5<span>year</span></h2>
                    <h2>60,000<span>miles</span></h2>
                    <p>Limited Warranty</p>
                </li>
                <li>
                    <h2>5<span>year/unlimited miles</span></h2>
                    <h2>24<span>hour</span></h2>
                    <p>Roadside Assistance</p>
                </li>
                <li>
                    <p>You have certain expectations when looking for a new car and one of the most important is that it will last, which is why we back every Hyundai with “America’s Best Warranty.” It’s our way of showing how much confidence we have in the quality of the vehicles we make so you’ll have the confidence of knowing that the joy of ownership is one that will last for today, tomorrow and years down the road.</p>
                    <a href="#">Learn more about our Warranty</a>
                    <p><small>*America’s Best Warranty claim based on total package of warranty programs. See dealer for LIMITED WARRANTY details.</small></p>
                </li>
            </ul>

Here is my CSS:

    ul,li {
  margin: 0;
  padding: 0;
  list-style: none;
}

li {
  height: 500px;
  width: 100%;
  display: block;
}

li h2 {
  font-size: 110px;
}

@keyframes fadeIn {
    0% {
        opacity: 0;
        margin-top: 20px;
    }
    100% {
        opacity: 1;
        margin-top: 0px;
    }
}

h2.fadeup {
    animation-name: fadeIn;
    animation-duration: 1s;
    animation-timing-function: linear;
    animation-delay: 0s;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
    animation-direction: normal;
}
li h2 span {
  font-size: 20px;
}

Here is my JS:

$(document).ready(function(){

    $('h2').each( function(){

        $(this).waypoint({

            handler: function(){

                $(this).addClass('fadeup');

            }

        });

    });

});

No errors are showing up, but nothing seems to be happening as expected.

Answer №1

The reference $(this) within the function $(this).waypoints ({.. actually points to the waypoint object, rather than the h2 element

Here is a suggested solution:

$('h2').each(function(){

    var currentElement = $(this);

    $(this).waypoint({
         handler: function(){
             currentElement.addClass('fadeup');
         }
    });
})

Answer №2

Ensure you include the necessary arguments when using the $.each function. Opt for element rather than this for your jQuery selections.

$(document).ready(function(){
    $('h2').each( function(index, element){
        $(element).waypoint({
            handler: function(){
                $(element).addClass('fadeup');
            }
        });
    });
});

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

How can we achieve the same functionality as React Native's { flex: 1 } in React JS?

I've been diving into React Native development, and now I'm exploring React.js for web applications. However, I'm facing a challenge in creating a simple View that takes up the entire screen to begin experimenting with different components. ...

Tips for showcasing devnagri script from user input on a php webpage

On one of my PHP pages, I have changed the font for input to Devnagri. Now, I need to display it on the next PHP page and also insert it into MySQL. The first page, index.php, contains the following details... <table width="535" height="54" border=" ...

Utilizing Google Tag Manager to save the identifier of a selected div or button

After searching high and low, and reading extensively, I still couldn't find the answer... I am managing a website where Google Tag Manager is in place, and I am trying to retrieve the ID of a clicked button (or its parent). This is my code snippet: ...

I'm looking to divide the background horizontally into two sections, with one side being a solid black color and the other side incorporating a gradient of two colors, such as purple transitioning into pink

body { height:100%; background: linear-gradient(top, #d808a4 50%, black 50%); background: linear-gradient(top, #d808a4 50%,black 50%); background: linear-gradient(to bottom, #d808a4 50%,black 50%); height: 229vh; } I am trying to creat ...

HTML: div element with a compact size

Is there a way to establish a minimum height for a div that also adjusts as new data is added? ...

Getting the most out of geometry vertices with Threejs: mastering partial transforms

In my current project, I am working with a mesh that consists of approximately 5k vertices. These vertices have been merged from multiple geometries into a flat array. Initially, I was able to modify individual vertices successfully by setting the flag `ve ...

Adjust the search box dimensions in the navbar back to its initial size

https://i.sstatic.net/RHAm0.png <form class="navbar-form navbar-left navbar-input-group" role="search" name="basicSearch" id="basicSearch"> <div class="form-group"> <div class="input-group" style=""> <input type="text" id=" ...

Node.js returns undefined when scraping using Osmosis

My current approach involves utilizing the Osmosis package for web scraping in the following manner: require('osmosis') .get('https://samdb.org/phones/galaxy-s10') .find("//h4[contains(text(), 'MP')]") .data(data=> ...

Run JavaScript code after the completion of loading a generic handler in ASP.NET

I am facing a challenge with a gridview that contains a list of pdf files. Whenever a user clicks on a pdf, it should display the file inline on the page. I have been trying to execute some javascript after the pdf has finished loading, but I'm encoun ...

Tips on incorporating a scrollbar into a table using PHP?

I need help figuring out how to add a scroll function to my table that is generated in a PHP file. I prefer not to implement the scroll feature in a separate style.css file, I want it to be directly embedded in the PHP file itself. Below is the code I ha ...

'The 'HtmlToDocx' class does not contain the method 'run' within its attributes

While troubleshooting, I encountered the error message "HtmlToDocx object has no attribute run" from the parser. My usual go-to for finding solutions is Stack Overflow due to its rated answers. Surprisingly, I could not find any helpful responses to this ...

Maintain the initial value using ko.mapping.fromJS

How can I prevent new data fetched from an AJAX call using ko.mapping.fromJS from replacing the existing data in my observableArray? I want to append the new data without losing the previous entries. function ViewModel() { var self = this; self. ...

What is the method for displaying data on a browser instead of downloading it when creating a link?

Clicking on this link will trigger the download of the f.txt file. Rather than downloading it, I would like the data to be displayed directly in the browser. For example, when you click this link, the data is shown directly in the browser. While I have ...

Guide on how to set a timestamp for an array type document object in Firebase

I am currently working on a Vue.js application that allows for dynamic addition of items to an array in Cloud Firestore. The array, named events, consists of various objects including a timestamp. Below is the function I am using: let newDetails = this.ne ...

Accessing the state value within the render method in ReactJS can provide crucial information

Just starting out with React and ran into an issue while trying to pass state in constructor to the render method. My h1 element is not showing up on the screen. Any idea what I might be doing wrong? class Mod extends React.Component { constructor( ...

Enhancing 2D video viewing with Threejs interactivity

I want to create an interactive 2D video using three.js and maintain the aspect ratio of the video when resizing the browser window. Here is the code I am currently using: var camera, scene, renderer; var texture_placeholder, distance = 500; init() ...

Having trouble sending data to a Laravel 5 controller through Angular. Keep getting Error 422: Unprocessable Entity

My login form is built with angularjs, and I am trying to send the user's credentials to the default AuthController in Laravel 5. However, I keep encountering a server response of 422 Unprocessable Entity with the message: email field is required. HT ...

What is the best way to save the output of a Node.js script into a MongoDB

I am looking to create a Node.js script that can ping a host and store the results in MongoDB. Currently, I am using Agenda to schedule the pings at specific times. However, I am having trouble figuring out how to save the execution results to the databa ...

Is it necessary for Angular Reactive Form Validator to convert types before checking the value for min/max validation?

Preface: My motivation for asking the questions below stems from my experience with form.value.purchaseCost. When the <input> field does not have type=number, I receive a string instead of a number. This required me to manually convert it to Number ...

Creating an expand and collapse animation in a `flex` accordion can be achieved even when the container size is fixed using

Good day, I am in need of a fixed-height HTML/CSS/JS accordion. The requirement is for the accordion container's height to be fixed (100% body height) and for any panel content overflow, the scrollbar should appear inside the panel's content div ...