Executing individual if/each statements to choose specific divs and modify the position of their background images separately

I am currently faced with the challenge of relocating a background image within a div based on the content of a span.

The complication arises from the fact that multiple divs share the same class and cannot be individually modified.

For instance, each cell where the image position needs to be adjusted appears as follows:

<td class="js-gridBlock js-Pre-order" data-attvalue1="3XL" data-attvalue2="Plum">
  <div class="js-gridImage">
    <div class="prodpageGridText" id="gridtext-gridbox34">
      <span class="status">Due in 4 weeks</span><br>
      <div class="prodpageGridTextArrow">
      </div>
     </div>
   </div>
</td>

Where the value within the Span class Status can range from 1 to 10.

My current jQuery script is as follows;

 jQuery(document).ready(function() {
      if (jQuery('span.status:contains("4")')) {
            jQuery('.js-Pre-order .js-gridImage').css("background-position", "-4px -213px"); 
        } 
      if (jQuery('span.status:contains("5")')) {
            jQuery('.js-Pre-order .js-gridImage').css("background-position", "-4px -242px"); 
        }
     if (jQuery('span.status:contains("10")')) {
            jQuery('.js-Pre-order .js-gridImage').css("background-position", "-4px -387px"); 
        }
    });

The issue I am facing is that when the jQuery script is executed, all js-gridImage elements end up with the same

("background-position", "-4px -387px")
, instead of individually changing based on their span values.

I have come across the .each() function, but I am unsure of the best way to implement it in my code.

Thank you in advance.

Answer №1

To utilize the connection between elements, you can employ the .closest(selector) function to move up the hierarchy and manipulate the CSS properties of the desired element.

When the jQuery document is loaded, the following code can be used:
jQuery(document).ready(function() {
    jQuery('span.status:contains("4")').closest('.js-gridImage').css("background-position", "-4px -213px"); 
    jQuery('span.status:contains("5")').closest('.js-gridImage').css("background-position", "-4px -242px"); 
    jQuery('span.status:contains("10")').closest('.js-gridImage').css("background-position", "-4px -387px"); 
});

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

The anchor tag causes the tooltip to display outside of the td element

One of my requirements is to display the click percentage for each link in an HTML template that is dynamically fetched from a database. I attempted to show the click percentage for each anchor link by adding them to the `data-title` attribute using jQuery ...

What is the reason behind the functionality of invalid HTML?

As someone diving into the world of web programming, I have a burning question: Why does invalid HTML seem to work just fine sometimes? And why is it that HTML validation isn't always necessary? <html> <body> </html> It's intr ...

table with flexible cell width and fixed table width

I've been troubleshooting this issue for days, but I just can't seem to find a solution! The problem lies within a table used on a webpage. If I don't specify table-layout, I can make one cell width dynamic, but I lose the ability to set &a ...

Tips for successfully implementing a basic JQuery code in Internet Explorer

Having trouble getting this to work on my website, especially in Internet Explorer. Check it out here: http://jsfiddle.net/b43hj/24/ Any tips on how to make it compatible with all browsers? Thank you edit - I've only used the code from the jsfidd ...

Deactivate a function when the dropdown input is selected

I'm currently using a function to attach scroll events to a slider element for navigating through slides. However, I want to temporarily disable this function in specific situations - such as when a dropdown is in focus - to allow smooth scrolling thr ...

When the page is scrolled to 50 pixels, a modal pop-up will appear

I attempted to use cookies to store a value when the user clicks on a popup to close it, ensuring that the popup does not show again once closed. However, I am encountering an issue where the popup continues to open whenever I scroll, even after closing it ...

Tips for displaying a digital keyboard when a webpage loads on iOS Safari

While developing a website, I noticed that the virtual keyboard fails to appear when an input field gains focus during the page load process. As per Apple's policy restrictions, this functionality is not allowed. However, I am interested in finding a ...

What is the best way to incorporate two foreach loops in Laravel within a single view?

<h2 class="section-subheading text-muted text-center">First Course</h2> <div class="row text-center"> @foreach ($data as $item) <div class="col-md-4 px-3"> <div cl ...

What is the reason for the presence of the ^ symbol in this drop-down menu?

Just came across a great script, check it out here! Noticed the drop down has a ^ on top. The CSS I found for this is as follows: #colorNav li ul li:first-child:before{ content:none; position:absolute; width:1px; height:1px; border:5 ...

Keeping track of the toggle state using a cookie

While searching for a way to retain the toggle state, I stumbled upon js-cookie on GitHub. The documentation provides instructions on creating, reading, and deleting a cookie. However, an example would have been really helpful in enhancing my understanding ...

Having trouble sending an array's JSON data to a web service using Angular

I am working with a table where each cell in the rows contains form fields. The table also has two buttons: one button adds a new row to the table, and the other sends all the rows. Below is the code snippet for adding new blank rows: $scope.attributes = ...

"Assure that setTimeout will be executed within the then method in

Within an Immediately Invoked Function Expression (IFFE), I am returning a Promise. The first thing that is logged is first, followed by third, and then second. Despite having the setTimeout inside the then block, shouldn't everything within it be exe ...

The resizing of iframes in Javascript is malfunctioning when it comes to cross-domain functionality

I have implemented a script to dynamically resize iframe height and width based on its content. <script language="JavaScript"> function autoResize(id){ var newheight; var newwidth; if(document.getElementById){ newheight=docume ...

Aligning a 900px wide element with an orange background on the left and a white background on the right

I have a section on my website that I want to position in the center of the screen. It's a simple task, but I have a specific design in mind. I want the left side of the section to have an orange background, while the right side should be white. I&apo ...

Accessing an object from an AngularJS controller in an external function

I previously inquired about this issue and received a suggestion to add a service, but it did not solve the problem. I am trying to access a variable from a controller ($scope) within an external function. Below is a snippet of the example: app.controll ...

How to disable annoying browser ad extensions using Javascript

Is there a way to prevent browser add-ons from injecting HTML code? My website built in angularjs is experiencing routing issues due to certain browser add-ons. The following HTML snippet is causing errors in my angularjs: <script async="" src="http:/ ...

Error encountered when attempting to retrieve data from a JSON object

I'm in the process of extracting some information from a JSON object, but I've encountered a confusing issue. The JSON object structure is as follows: { "status": "success", "data": { "image": null, "video": null, "author": null, "publisher": "M ...

Scraping Websites with SimpleHTMLDom in PHP

I am struggling to extract specific data from a table on a website page, particularly the columns name, level, and experience. The table's alternating row colors (zebra pattern) are complicating this task. I have implemented SimpleHTMLDom for this pu ...

Creating a dynamic dropdown menu in HTML to showcase a variety of images

I am trying to create a drop down menu where each selection displays multiple elements. For example, if sensor 1 is chosen, I want to show a picture of its location and its address. I am having trouble figuring out how to add these functions to the drop ...

Retrieving JSONP data from Wunderground using jQuery

Having trouble parsing data from the "wunderground" API using JavaScript and Ajax. Some values are returned successfully, while others are not. Here is the link to the API I am utilizing: { "forecast":{ "simpleforecast":{ "forecastday":[ ...