Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9133489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:29:52+00:00 2026-06-17T08:29:52+00:00

I’m trying to dynamically change the background of my page using the jquery waypoint

  • 0

I’m trying to dynamically change the background of my page using the jquery waypoint plugin (which tells you when an element is at the top or bottom of the viewport). My HTML is simple – I have multiple headings with the id of #part1',#part2′ and so on and so forth. When part[x] is at the top of the viewport, I want img[x] to be displayed.

The following code works:

$("#part2").waypoint(function(event, direction) {
   if (direction === 'down') {
      $(".container").attr("id", "img2");
   }
   else {
      $(".container").attr("id", "img2");
   }
});

However, I have 18 ‘parts’ and I don’t want this code 18 times. I tried using a for loop
like this:

var chapters = $("[id^='part']");

for (var i=1; i<=chapters.length; i++){

    $("#part[i]").waypoint(function(event, direction) {
       if (direction === 'down') {
          $(".container").attr("id", "img[i]");
       }
       else {
          $(".container").attr("id", "img[i]");
       }
    });
}

But again, not working. Is it possible to escape the selector with square brackets like I’ve done? If not, what’s the correct way to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:29:53+00:00Added an answer on June 17, 2026 at 8:29 am

    Assuming the waypoint plugin is written correctly and handles multiple elements:

    $("[id^='part']").waypoint(function(event, direction) {
        $(".container").attr('id', this.id.replace('part', 'img'));
    });
    

    add direction checks as required.

    I’m not familiar with this plugin but the key part is that you should be able to bind the waypoint plugin to multiple elements, and then within the event handler this should be the element that triggered the event, whose ID is then just this.id.

    NB: just changing the ID on .container wouldn’t normally change the image on it, unless you’ve got a stack of CSS rules that look like:

    #img1 { background-image: ... }
    #img2 { background-image: ... }
    ...
    

    However in that case you should use classes instead of IDs – it’s much more normal for the ID of an element to be constant through its lifetime and for its CSS classes to change.

    If .container has no other CSS classes it would make sense to give it a fixed ID of container instead and use a variant of the above code:

    $("[id^='part']").waypoint(function(event, direction) {
        $("#container").attr('class', this.id.replace('part', 'img'));
    });
    

    The constraint on not having any other CSS classes is that since class can contain multiple values it’s far simpler to just completely overwrite the class list than it is to remove the previous class (whatever that was) and then add the new one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to select an H1 element which is the second-child in its group
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm making a simple page using Google Maps API 3. My first. One marker
I am reading a book about Javascript and jQuery and using one of the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.