What is the method to apply a class exclusively to an img tag?

While using the Cycle2 Carousel plugin, I encountered an issue with the class being set for my thumbnails. The plugin sets the .cycle-pager-active class for both images and divs, which is not ideal. I attempted to fix this by adding 'img' before the class, but nothing changed. If you inspect the #thumbnail div at the bottom of the body tag, you'll see the problem. What should I do next?

What I tried so far:

I modified this line:

pagerActiveClass: 'cycle-pager-active',

to this line:

pagerActiveClass: 'img.cycle-pager-active',

but unfortunately, the issue persisted.

$(document).ready(function(){

   $(".mySlideShow").cycle({
       pauseOnHover: true,
       pagerActiveClass: 'cycle-pager-active',
       pager: "#thumbnail",
       pagerTemplate: "<img src='{{children.0.src}}' width='70' height='70'>",
       slides: ".item"
    });
  
});
.mySlideShow img{
  width:700px;
}
#thumbnail img {
  margin:10px;
}
.cycle-pager-active{
  border:3px solid orange;
}
<div class="mySlideShow">
    <div class="item">
      <img src="http://cdn.anitur.com/web/images/h494/2017-03/otel_armonia-holiday-village-spa_tZuhzJnp6BDndutoN1lV.jpg" alt="">
    </div>

  <div class="item">
      <img src="http://cdn.anitur.com/web/images/h494/2017-03/otel_armonia-holiday-village-spa_M6XtiCxv8AvkGako7aHr.jpg" alt="">
  </div>
  
  <div class="item">
    <img src="http://cdn.anitur.com/web/images/h494/2017-07/otel_armonia-holiday-village-spa_EOUfYFhHhV3UoxBxYTAr.jpg" alt="">
  </div>
</div>
  
<div id="thumbnail">
    <div class="thumbnail-expand"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.cycle2/2.1.6/jquery.cycle2.min.js"></script>

Check out the Codepen Demo

Answer №1

While I believe it would be more beneficial to adjust your HTML structure to ensure the thumbnails div is exclusively used for images, here is a solution that directly addresses your initial query :)

Adjust the addClass method

(function() {
  var originalAddClassMethod = jQuery.fn.addClass;

  jQuery.fn.addClass = function() {
    // Call the original method.
    var result = originalAddClassMethod.apply(this, arguments);

    // Trigger a custom event
    jQuery(this).trigger("cssClassChanged");

    // Return the original result
    return result;
  };
})();

Next, listen for the cssClassChanged event and remove the active class from non-img elements

$(document).ready(function() {
  var $slider = $(".mySlideShow").cycle({
    pauseOnHover: true,
    pager: "#single-pager",
    pagerTemplate: "<img src='{{children.0.src}}' width='70' height='70'>",
    slides: ".item",
    pagerActiveClass: "cycle-pager-active"
  });

  $("#single-pager > :not(img)").bind("cssClassChanged", function() {
    $(this).removeClass("cycle-pager-active");
  });
});

Check out the DEMO: https://codepen.io/anon/pen/ZJjEGQ

Answer №2

To begin, simply construct a div with a child div containing the thumbnail.

<div class="outer-container">
 <h1> What a lovely thumbnail! </h1>
 <div id="thumbnail"></div>
</div>

The code provided will apply the active class only to the elements inside the #thumbnail

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 Google Maps API has been successfully initialized, however, it is experiencing difficulties being displayed on the webpage

I have been successfully using a custom Google API on various web pages. However, I encountered an issue where the map API loads successfully but is not displaying on a specific web page. Below are the relevant code snippets: <html> <head> &l ...

"Troubleshooting why React fails to update an array state

When I click the update name button, the state is not changing properly using setState. I want to update the persons variable and have it reflect in the DOM as well. Any ideas on how to achieve this? App.js import React, { Component } from "react"; impo ...

Tips for adjusting a web address provided by a user

I have a JavaScript code that prompts the user to input a URL and then appends it to a div. The code is working fine, but now I am trying to add functionality to edit the entered URL by changing the width and height of any iframes before appending them. ...

Is there a way to effortlessly generate a mini-website from Markdown files?

Can a tool be found that creates a mini-website from locally-stored Markdown files complete with automatically generated navigation? I'm hoping for a service that can sync with my Dropbox, analyze the file structure, read the Markdown files, and effo ...

Organize the strings by first sorting them alphabetically based on the first letter and then by length starting from the longest. Next, arrange the strings in ascending

After experimenting and seeking help on stackoverflow, I have managed to sort an array of objects based on the 'plate' key using the following function: sort(function(a, b) { return a.plate.toLowerCase() > b.plate.toLowerCase() ? a.pla ...

Set the height of the div to match the length of the downward swipe

I am working on gradually revealing a div as the user swipes down on the body element. Currently, I am using jQuery along with hammer.js to detect the swipe gesture, but I need assistance in determining the distance of the swipe and adjusting the height o ...

Encountered an error message stating "This dependency was not found" after installing a package using npm

I recently added a package called htmldiff to my Vue project using the npm install command. The next step was to try importing the package into one of my components. import { diff } from 'htmldiff'; // note that the package does not use default ...

Navigating through a 3D world with just the tilt of

I am currently developing an immersive VR web application using three.js. I have integrated the device orientation controls from the Google Cardboard three.js demo for camera navigation. Now, I am looking to incorporate keyboard controls for additional fu ...

using configureStore instead of createStore

After updating my packages, I received a notification from VScode indicating that the createStore function was deprecated. This prompted me to go ahead and replace it with the recommended alternative. In my store file, I left the original line as a commen ...

Struggling to design a responsive layout as the div on the right keeps overlapping the div on the left

I recently built a React component that consists of two columns. In the left column, there's a calendar while the right column contains some text along with an input and select field. However, I noticed that when I resize the window, the elements on ...

Disabling the buttons within a div causes them to vanish from view

One issue we're facing is that some users are clicking the alert button multiple times. To address this, I'd like to implement logic at the specified line "Add logic here...". Upon making an ajax call, I want all buttons within that div to be dis ...

The Node.js express seems to be unable to fetch the css and js files

Sharing my main.ts file in which I am facing issues with linking my css and js files. view image import express from 'express'; import {Request,Response} from 'express'; import expressSession from 'express-session'; import pat ...

Executing form validation upon submission of a form from the view in BackboneJS

Recently, I delved into using Backbone.js to organize my JavaScript code and create modular applications. However, I encountered a snag when dealing with events. My goal is to develop a simple View that can handle forms and validate them. Eventually, I pl ...

Removing page scrolling in Bootstrap 5: A step-by-step guide

I would like to ensure that only the card-body block is scrollable. Currently, the page does a bit of scrolling when the card-body block overflows (resulting in 2 scroll bars). The lorem text was added as an example of an overflow. The desired outcome is d ...

Is it possible that attaching the click event directly to the ID is effective while attaching it to the childnode is not working

Can anyone help me with a problem I'm having? When I try to target a specific element by accessing its child nodes, the click event doesn't work. However, if I use getElementById and then attach the click event through that method, it works. Idea ...

JavaScript for rotating an element upon button click

My webpage design <div id="yabanner"> <img src="img/ok.jpg"> </div> <button>Click Me</button> <button>Click Me</button> <button>Click Me</button> My script code var button = document.getElementsBy ...

Guide on importing CDN Vue into a vanilla Typescript file without using Vue CLI?

In the midst of a large project that is mostly developed, I find myself needing to integrate Vue.js for specific small sections of the application. To achieve this, I have opted to import Vue.js using a CDN version and a <script> </script> tag ...

Encountering an issue while attempting to iterate through JSON response

After receiving a JSON response from the server: [{"id":605,"vote":1},{"id":606,"vote":-1},{"id":611,"vote":1},{"id":609,"vote":-1}] I attempt to iterate through the results and access the properties of each object: success: function (data) { $.each(da ...

Struggling to add custom styles to an Ionic radio button

I'm struggling to adjust the position of the icon in an Ionic radio button so that it sits a bit higher, but I can't seem to figure out how to do it. Below is the code snippet for reference: // HTML <ion-radio class="radio-input" mo ...

What is the best way to incorporate Ajax into a Rails application?

Check out the Webpage Design I am currently working on a Todo List that includes various Todo Items. Each incomplete task has a button called "Mark Item as Done" next to it, which triggers the button_to method when clicked. I am facing challenges in imple ...