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>

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

The only numbers that can be typed using Typescript are odd numbers

I am looking for a way to create a type in Typescript that can check if a value is an odd number. I have searched for solutions, but all I find are hardcoded options like odds: 1 | 3 | 5 | 7 | 9. Is there a dynamic approach to achieve this using only Types ...

Error: Attempting to update the value of 'ordersToDisplay' before it has been initialized in a re-render of React. This results in an Uncaught ReferenceError

Trying to dynamically update the document title to include the order number by clicking a button to display different numbers of orders from an array on the screen. The process involves importing a JSON file, filtering it based on user input, calculating ...

Contrasting the uses of element(...) versus element(...).getWebElement() in Protractor

What is the advantage of using element(...).getWebElement() instead of element(...) when they both perform similarly? Why are there two different APIs for the same purpose? ...

Verify in JavaScript if the script is executing within a WinStore (WinJS) program

I am in the process of developing a JavaScript library that is compatible with both Windows Store (WinJS) applications and traditional HTML/JavaScript apps. The dependency I am utilizing loads dynamically and has separate SDKs for WinJS apps and standard w ...

JavaScript - Capture user input and store it in a cookie when the input field is changed

Any help with my issue would be greatly appreciated. I'm currently working on saving the value of a form input type="text" to a cookie when the input has changed. It seems like I'm close to getting it right, but unfortunately, it's not worki ...

Removing an element from an object using ng-model when its value is empty is a standard practice

Utilizing the $resource service to access and modify resources on my API has been a challenge. An issue arises when the ng-model-bound object's value is set to empty - the bound element is removed from the object. This results in the missing element ...

Thumbnail Carousel Caption in Bootstrap 3

Is it possible to create a Bootstrap 3 carousel where the image changes when the mouse cursor hovers over the thumbnail under the carousel? Currently, I have a setup with the carousel and thumbnails below it. Typically in Bootstrap, the image changes autom ...

Issue with accessing container client in Azure Storage JavaScript library

When working on my Angular project, I incorporated the @azure/storage-blob library. I successfully got the BlobServiceClient and proceeded to call the getContainerClient method, only to encounter this error: "Uncaught (in promise): TypeError: Failed ...

How to efficiently send multiple objects in response to a single GET request with Express and Node.js

The code snippet I am working with looks like this - sendServer.get('/download',function(request,response){ var status="SELECT * from poetserver.download where status='0'"; console.log(status) connection.query(status,function(error,ro ...

suggestion of numerous values in input box through jquery autocomplete

I'm looking for some assistance in modifying my PHP code to allow users to receive auto-suggestions for multiple values. Currently, it functions well for one value, but I would like users to receive auto-complete suggestions for more values (for examp ...

"Compilation error: 'not defined' is not recognized, 'no-undef

I am currently working on a login form that will fetch values from this API: However, the password field is currently empty, allowing any password to be accepted. This results in the error: Failed to compile 'userName' is not defined no-undef; ...

What are the possibilities of utilizing a variable that is stated within composition to enable dynamic rendering?

I'm working on a Vue.js v3 project using the composition API. I have defined a variable in my setup like this: setup() { const showInvoiceItemForm = true; return { showInvoiceItemForm }; }, Now, I want to show a form when a button is click ...

Troubleshooting: Css navbar alignment

How can I center the navigation bar both horizontally and vertically? I've tried various methods but nothing seems to work. Here is my HTML: <section class="navigation" ng-controller="NavController"> <div class="nav-container"> ...

Console.log is displaying array as [object Object] when utilizing Typescript

When working with an object in typescript called "obj," I encountered a strange behavior. Initially, when I ran the console.log(obj); command, the output in the terminal console was displayed as [object Object]. However, after wrapping it in JSON.stringify ...

What is the significance of the dollar sign prefix ($) in Vue.js?

Can you explain the significance of the dollar symbol prefix used before property names in Vue.js? For instance, consider this code snippet: this.$emit('clicked', 'demo') ...

Introduction to the CodeIgniter wkhtmltopdf library

Seeking assistance here - I am attempting to generate a printable pdf report for a calendar using Codeigniter and the fullcalendar jquery library. I have previously used mpdf successfully for creating pdf reports, but when applied to my calendar page, it ...

Node.js is known for its unreliable promise returns

Currently, I have a function in place that establishes a connection with a sql database. After querying the database and formatting the results into an HTML table, the function returns the variable html: function getData() { return new Promise((resolv ...

What are some ways to maintain code efficiency when working with AJAX requests?

Looking at the code below, I am making two identical Ajax requests with only one line of difference. Is there a way to consolidate this into a function to maintain DRY (Don't Repeat Yourself) code? $('.searchable').multiSelect({ selecta ...

Associate the ng-model with ng-options value and label

I'm currently using ng-options to create a select tag that displays location options. The labels represent the location names, while the values indicate the corresponding location ID as stored in the database. In addition to binding the value (locati ...

React: Struggling to retrieve the current inputbox event value

I have encountered an issue while working on a customized input box in react. Despite my efforts, I am unable to access the current event value. Here is the relevant code snippet: Parent Component:---------------------------------------------------------- ...