简体   繁体   中英

jQuery- Loop through buttons add active class on click

在此处输入图片说明 Good day! I'm new with jQuery. Please how do I loop through buttons to bring out only its contents when I click on it, and change the active class on them each time I click.

I repeated the code below for two more buttons:

$("#member-company-btn").on("click", () => {
    $("#member-company-content").show("slow");
    $("#group-company-content").hide("slow");
    $("#abuja-sub-company-content").hide("slow");
    $("#lagos-sub-company-content").hide("slow");
})
$("#group-company-btn").on("click", () => {
    $("#member-company-content").hide("slow");
    $("#group-company-content").show("slow");
    $("#abuja-sub-company-content").hide("slow");
    $("#lagos-sub-company-content").hide("slow");
})

This to add active class to only the clicked button didn't work.

$("#subsidiary-btns .subsidiary-btn").on("click", () => {
    $("button.subsidiary-btn").removeClass("subsidiary-btn-active");
    $(this).addClass("subsidiary-btn-active");
})

Thank you

Today I decided to stay on this till it works, instead of styling differently. And Persistence paid off. Inspired to make some changes to the code. Below are the images showing a possible solution.

html代码

css代码

javascript代码

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM