JavaScript JQuery how to add only the clicked image css class with nth-child? jquery only clicked element,jquery toggle only clicked element,jquery

Instead of .attr('class', 'active'); you should use .addClass('active'); and you don't need index you could add class directly to the parent :

$(".thumbnail").click(function () {
    //remove class 'active' from all the lis
    $(this).parent().siblings().removeClass('active'); 

    //Add class 'active' to the clicked one
    $(this).parent().addClass('active');
});

Hope this helps.

Answer:1

Looking at your code you want to add the class at the li level rather than at the img level.

try

$(".thumbnail").click(function () {
   $(this).parent().addClass( 'active'); //go to the parent of the current element and add class active
});

Instead i want only the clicked img to get the class "active"

if you want to add class at img level, then try this code

$(".thumbnail").click(function () {
   $(this).addClass( 'active');
});
Answer:2

Might be easier to understand:

var selector = $('.product-thumbnail img.thumbnail');
// select img tag with thumbnail class who are descendants of class product-thumbnail

selector.click(function(){
    selector.removeClass('active'); // remove class from all selector
    $(this).addClass('active'); // add class to clicked
});
Answer:3

I'm trying to add additional function to the Gtranslate module in Drupal to make it as a language switch block. When click the gtranslate block, the url will also redirect to the desired language ...

I'm trying to add additional function to the Gtranslate module in Drupal to make it as a language switch block. When click the gtranslate block, the url will also redirect to the desired language ...

Using the Javascript, Vue.js and Typeahead.js way shown by Jeffery Way and Algolia docs to index json objects from the Algolia platform. Currently when i search for the result i require and hit the ...

Using the Javascript, Vue.js and Typeahead.js way shown by Jeffery Way and Algolia docs to index json objects from the Algolia platform. Currently when i search for the result i require and hit the ...

I’m pretty new to backbonejs and i’m trying to create a basic application. The application is something like this: I have 5 sections: A, B, C, D and E Each section has 2 radio buttons. Section A - ...

I’m pretty new to backbonejs and i’m trying to create a basic application. The application is something like this: I have 5 sections: A, B, C, D and E Each section has 2 radio buttons. Section A - ...

Simply, i'm trying to use jQuery to append a numeric value (currently stored in an array) to the end of the "id" attribute of a number of specified elements. $('#headerAfirstLink, #headerAsecondLink, ...

Simply, i'm trying to use jQuery to append a numeric value (currently stored in an array) to the end of the "id" attribute of a number of specified elements. $('#headerAfirstLink, #headerAsecondLink, ...

  1. append array value in php
  2. append array value in javascript
  3. append array value in jquery
  4. append array value python
  5. append array value
  6. append array value js
  7. append array values swift
  8. append array values numpy
  9. append value array bash
  10. adding array value in php
  11. append values array java
  12. adding array values in javascript
  13. adding array values in java
  14. adding array values
  15. adding array values python
  16. adding array values in c#
  17. append array key value php
  18. c append array value
  19. python append array values to list
  20. vba array append value