JavaScript Adding class on clicking an item

I have am trying to create jQuery code which adds a active-menu class to a link whenever I click on it. However I need to check first if it already has the class before adding. I only need the link that was clicked to get the class.

<%= link_to "Testimonials", root_path, class: "nav-link js-nav", :"data-id" => "testimonials" %>
$(document).on('click', function() {
  var item = $('.js-nav');
  if (item.hasClass('active-menu')) {
    item.removeClass('active-menu');
  } 
  item.addClass('active-menu');
});

I thought this would work but as of now it puts active-menu on all the items that has js-nav. Again, I only need to add the class if it doesn't have it yet and only need to add on the click item.

Answer:1

Try this

$(document).ready(function () {
    $(this).on('click', '.js-nav', function() {//  here $(this) is refering to document
        $(this).addClass('active-menu');
    });
});
Answer:2

Set the class on $(this) instead of every element that matches class

Also, add class in the else conditions, otherwise it will add class even after removing it

Like this

$(document).on('click', function(){
    var item = $(this);
    if (item.hasClass('active-menu')){
      item.removeClass('active-menu');
    } else{
      item.addClass('active-menu');
    }
});
Answer:3

On my website, I have an slider with images. In the backend, the current slider images are displayed as well as a button that adds an input field, so I can upload new images for the slider. The code ...

On my website, I have an slider with images. In the backend, the current slider images are displayed as well as a button that adds an input field, so I can upload new images for the slider. The code ...

  1. get file from form php
  2. save file from form php
  3. file itr from form 16
  4. upload file from form
  5. php file from form
  6. upload file from form php
  7. upload file from form ajax
  8. send file from form
  9. download file from form
  10. post file from form
  11. submit file from form
  12. file from html form
  13. curl file from form
  14. file form 941 online
  15. file form d
  16. file form 941
  17. file form 990-n
  18. file form 1040
  19. file form 7004 online
  20. file form 4868 online

I have the form disabled when click edit form is enable <% while(resultset.next()){ %> <form method='POST' action='EditCompany?id=<%= resultset.getString(1)%>'> <tbody> &...

I have the form disabled when click edit form is enable <% while(resultset.next()){ %> <form method='POST' action='EditCompany?id=<%= resultset.getString(1)%>'> <tbody> &...

  1. enable button when form is valid angular
  2. enable button when form is valid
  3. enable submit button when form is valid
  4. enable button if form is valid angular 6

I am using testcafe version - 1.6.1 I am trying to apply an assertion to see if an element is clickable or not (expect to not be clickable). I've found the elements class name and a dynamic sibling ...

I am using testcafe version - 1.6.1 I am trying to apply an assertion to see if an element is clickable or not (expect to not be clickable). I've found the elements class name and a dynamic sibling ...

  1. hasclass doesn't work
  2. jquery hasclass doesn't work
  3. jquery doesn't hasclass

I have tried empty validation using ternary operator, but it's not returning data. Below is my json object var data = [ { "name": "test", "domain": "domain1", "subdomain": "...

I have tried empty validation using ternary operator, but it's not returning data. Below is my json object var data = [ { "name": "test", "domain": "domain1", "subdomain": "...