user3817083
user3817083

Reputation: 79

Javascript: Find Other IDs

I have 5 divs with a class of "ticket-selection" and unique IDs. I figured out how to find each unique ID on click, and add a class. However, I want to find all the other IDs, and remove that class (so only 1 ID has the "active" class). Here's my code:

        var $ticket;

        $('.ticket-section').on('click', function(e) {
            $ticket = $(this);
            if (this.id) {
                alert(this.id);
                $(this).toggleClass("active");
            }
            e.preventDefault();
        });

I believe I would need to create an array, but I'm not sure how that integrates into this code. Can I say "if this ID is clicked, addClass, but removeClass on the other 4 IDs. Thank you.

Upvotes: 0

Views: 36

Answers (2)

ahmadMarafa
ahmadMarafa

Reputation: 1346

 $('.ticket-section').on('click', function(e) {
        $(".active").removeClass("active") ;
        $(this).addClass("active");

    });

Upvotes: 1

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79840

You could remove all active class from .ticket-section using remove class and add to this in the click handler.

$('.ticket-section').removeClass('active');
$(this).addClass('active')

Upvotes: 4

Related Questions