Changing the image source when clicking on it and removing it can be done by using JavaScript

How can I add a class 'selected' to ".swiper-slide" when it is clicked?

I also want to change the image src when svg-img1, 2, or 3 is clicked.

However, I need the image to revert back to its default src when another swiper-slide is clicked.


$('.swiper-slide').on('click', function() {
    $('.swiper-slide').removeClass('selected');
    $(this).addClass('selected');
});

jQuery(document).ready(function($) {
    $('#swi-image1').on({
        'click':function() {
            if($('#svg-img1').hasClass("selected")) {
                $('#svg-img1').attr('src','/img/front/photos-white.svg');
            } else {
                $('#svg-img1').attr('src','/img/front/photos.svg');
            }
        });
    
    $('#swi-image2').on({
        'click':function() {
            $('#svg-img2').attr('src','/img/front/youtube-white.svg');
        }
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="swiper-container pt-2 pb-4" id="contents-swiper">
<div class="swiper-wrapper">
<div class="swiper-slide" id="swi-img1"><img src="/img/front/photos.svg" id="svg-img1">card</div>
<div class="swiper-slide" id="swi-img2"><img src="/img/front/youtube.svg" id="svg-img2">youtube</div>
<div class="swiper-slide" id="swi-img3"><img src="/img/front/microphone.svg" id="svg-img3">cast</div>
</div>
</div>

https://i.sstatic.net/H2UNm.png

https://i.sstatic.net/l37hj.png

Answer №1

Give this a try.

$('.swiper-slide').on('click', function(){
  $('.swiper-slide').removeClass('selected');
  $(this).addClass('selected');
  if($('#swi-img1').hasClass("selected")){
    $('#svg-img1').attr('src','/img/front/photos-white.svg');
  }else{
    $('#svg-img1').attr('src','/img/front/photos.svg');
  }
  
  if($('#swi-img2').hasClass("selected")){
    $('#svg-img2').attr('src','/img/front/youtube-white.svg');
  } else {
    $('#svg-img2').attr('src','/img/front/youtube.svg');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="swiper-container pt-2 pb-4" id="contents-swiper">
<div class="swiper-wrapper">
<div class="swiper-slide" id="swi-img1"><img src="/img/front/photos.svg" id="svg-img1">card</div>
    
<div class="swiper-slide" id="swi-img2"><img src="/img/front/youtube.svg" id="svg-img2">youtube</div>
    
<div class="swiper-slide" id="swi-img3"><img src="/img/front/microphone.svg" id="svg-img3">cast</div>
</div>
</div>

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

What is the best way to pass a variable to the chrome.tab.create function?

Is there a way to pass a variable to the `chrome.tabs.create` function? I am currently working on setting up event listeners for my anchors, but I am faced with a challenge as I am creating them within a for loop: for (var i = 0; i < links.length; i++) ...

What is the best way to show HTML code on a REACT website without disrupting the existing styles?

I am trying to showcase the following code in a REACT webpage, but it is not displaying as code. Instead, it is showing as actual elements. How can I display the button codes below as actual code? <code> <pre> <Button className='btn-grv ...

Error: AngularJS form validation is not working properly

I've been following the AngularJS documentation, but I'm having trouble with my validations. The error messages are not showing up and the minimum length restrictions for the form fields are not working: <form name="callbackForm" ng-submit="r ...

What is the reason for jQuery returning an integer instead of an object?

Currently, I am attempting to preselect an option within a <select> tag based on a variable. However, while using jQuery to select the elements and iterating through them with .each(), it appears to be returning integers instead of objects, making .v ...

Values returned by XmlHttpRequest

When it comes to returning data from an XmlHttpRequest, there are several options to consider. Here's a breakdown: Plain HTML: The request can format the data and return it in a user-friendly way. Advantage: Easy for the calling page to consume ...

Using jQuery to create a smooth transition effect between two blockquote elements

Need help with fading between two blockquotes using jquery. Looking for a continuous fade transition around 10 seconds. Is it possible to achieve this effect with jquery? ...

Error: Unable to assign value 'auto' to property of null object

Whenever I attempt to update my MongoDB using AngularJS on the front end and Node.js on the backend, I encounter a `TypeError: Cannot set property 'auto' of null error. Here is the code snippet that resulted in this issue: AngularJS Code: scope ...

Is it possible to dynamically create and add new partitions to an existing topic in Node.js?

I am currently utilizing the "kafka-node" module to communicate with a kafka server, but I am unable to determine how to increase the number of partitions in an existing topic. For instance, I need to modify it from 4 partitions to 5. ...

Utilizing HTML5 canvas to extract pixel data from an image loaded from an external source

When it comes to security reasons, doing it directly may not be feasible. Nevertheless, there are rumors circulating about certain image-hosting platforms that permit the use of their images in a comparable way (could Google Picasa be one?). I might be mis ...

Issue with Chrome not triggering onMouseEnter event when an element blocking the cursor disappears in React

Important Note: This issue seems to be specific to Chrome Currently, React does not trigger the onMouseEnter event when a blocking element disappears. This behavior is different from standard JavaScript events and even delegated events. Below is a simpli ...

When using position:sticky, the overflow:auto behavior may not function as intended

My Bootstrap table has the class "table-responsive," which automatically sets overflow-x to auto when the table is too wide for the screen. In addition, I've applied the class "sticky-top" to the th elements in order to make them sticky within the ta ...

What is the best way to retrieve an array of other models from a parent model in Mongoose using queries?

Two Schema exist for user and todo. Each todo is associated with an owner who is a user, and each user has an array of todos. // user.js const TodoSchema = require('./todo').TodoSchema; var UserSchema = mongoose.Schema({ name: { type: String, ...

Difficulty with alignment in the process of generating a vertical stepper using HTML and CSS

I'm currently working on developing a vertical stepper component using html & css with inspiration from this design. However, I've encountered some alignment issues in the css that I'm struggling to resolve. CSS .steps-container .step::b ...

Enhancing State in React Component through Prop Update

I am aiming to achieve the functionality of clicking a button in a child component and having that component removed. I am new to React and currently in my app.js file, I have a component with a prop like this: <IntroSteps isHidden={false} /> Inside ...

Is there a way for me to obtain the present value of the chosen button in the list below?

I am working with a group of three buttons labeled English, Hindi, and Urdu. How can I retrieve the value of the selected button in a JavaScript function? <div class="btn-group" data-toggle="buttons"> <label class="btn btn-primary active"> ...

Transitioning between sections by clicking on a navigation menu item

I'm looking to create a cool animation effect on my website. When a user clicks on a specific menu link, such as "about-section," I want the page to smoothly scroll down to that section in a parallax style. If anyone knows of a jQuery plugin that cou ...

Avoiding flickering when the browser back button is clickedAvoiding the flickering effect

As I work on developing an Asp.Net application, a challenge has arisen. In order to prevent users from navigating back to the login page after logging in, I have implemented JavaScript code successfully. However, when clicking the browser's back butto ...

Showing an error message without causing any displacement of elements positioned underneath

https://i.sstatic.net/2QvQb.jpg Is there a way to insert an error message text without displacing the element by 4 units using CSS or Javascript? ...

Deactivating the Grid and its offspring in React.js MUI

Is it possible to Disable the Grid (MUI Component) and its Children in React js? Additionally, how can we Disable any Container and its items in React js, regardless of the type of children they have? For example: <Grid item disabled // ...

What is the reason for the index.html file not connecting to the local .css files and .js file?

I'm currently using node.js to send an .html file that includes the following tags: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="description" content="This is my personal profile website" /> <link r ...