Adding a Class with jQuery On Click Event

Trying to troubleshoot why this code isn't functioning:

https://jsfiddle.net/bv6ort3g/

HTML:

<div class="all-btn">All Button</div>

<div class="item all"> item 1 </div>
<div class="item all"> item 2 </div>
<div class="item all"> item 3 </div>
<div class="item all"> item 4 </div>
<div class="item all"> item 5 </div>

CSS:

.all-btn {
    cursor: pointer;
    padding: 5px;
    border: 1px solid #000;
    display: inline-block;
    margin-bottom: 15px;
}

.item {
    display: none;
}
.show {
    display: block;
}

jQuery:

$('.all-btn').on('click', function () {
    $('.all').addClass('show');
});

I aim to click the button and have it add the class show to all div elements with the class all

Can anyone point me in the right direction?

Appreciate any help.

Answer №1

Essentially, I have accomplished this...

$(function(){
    $(".item").hide();
    $(".all-btn").on("click", function(){
        $(".item").show();
    });
});

Alternatively, you can utilize this method to switch between displaying and hiding classes...

$(function(){
    $(".item").hide();
    $(".all-btn").on("click", function(){
        $(".item").toggle();
    });
});

I have maintained the same structure of your HTML elements...

<div class="all-btn">Show Button</div>

<div class="item all">Item 1</div>
<div class="item all">Item 2 </div>
<div class="item all">Item 3 </div>
<div class="item all">Item 4 </div>
<div class="item all">Item 5 </div>

However, I have taken out the .item and .show classes, while preserving your .all-button CSS style, although it is not essential for the functionality.

Check out the Demo: https://jsfiddle.net/bv6ort3g/1/

For a more elaborate demonstration, visit: https://jsfiddle.net/bv6ort3g/2/

Answer №2

If you have specific CSS rules in place, you can utilize the toggleClass() method to switch between two classes.

 $('.all-btn').on('click', function () {
   $('.all').toggleClass('show item');
 });

Alternatively:

$('.all-btn').on('click', function () {
    $('.all').toggle();
}); 

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

Issues with implementing Bootstrap Collapse functionality on dynamically generated content using Ajax

I am utilizing Ajax to retrieve some [information] which will then be used to set attributes within a duplicated div. Contained within the div is a bootstrap accordion: <div class='accordion-toggle' data-toggle='collapse' data-targe ...

CSS- The ultimate guide to perfectly centering a webpage without the need for extra margins!

Our SharePoint 2013 generated web page has a width of 1024. The main content div is styled as follows: #container_master { width:1024px !important; margin-left: auto !important; margin-right: auto !important; background-color:#FFF !impor ...

What is the best method for setting the height of an empty inline block element to match the line height of its container?

By default, inline-block elements adjust their height based on the container's line height only when they have content. However, I've noticed that empty or whitespace-only inline block elements default to a height of 0 (observed in Firefox). Is ...

Display numerous divisions depending on the choices made in several Select2 fields

I need assistance in creating a website without using check-boxes, and I believe jQuery Select2 is the solution. However, I am facing an issue where I cannot display multiple divs based on multiple Select2 selections. For instance, if "OnBase" is selected ...

Develop a dynamic animation using gradient and opacity properties

I'm still getting the hang of HTML, JavaScript, and CSS but I recently made some changes to someone else's code to animate a gradient. The original code used background: rgb, but I switched it to background: rgba. It seems to be working fine, but ...

Steps for updating the <option> selection in the third <select> menu when the second <select> menu changes, with the second menu being populated by altering the value of the first dropdown through jQuery AJAX

I'm facing a unique situation in my school management project and have been unable to find a solution online. Here's the issue: I have three <select> elements - the first one is for selecting a class, the second one is for selecting a secti ...

Making AJAX calls without displaying any alerts or prompts

I'm utilizing the $.when().then(success, fail).always() pattern to manage 3 asynchronous ajax calls. Here is the code I am using: $.when( $.ajax({ url: NewsCategoryUrl, beforeSend: function () { //alert('NewsCate ...

Take out the "href" attribute from the parent element

I'm looking to disable the link for the "parent li" in a dropdown menu when viewed on mobile. Here's the HTML structure: <ul> <li class="parent"></li> <li class="parent"></li> <li class="parent"></li&g ...

The Bootstrap modal will not appear when the parent container's position is fixed

I am having an issue with a bootstrap modal that I am using as an instruction guide. My goal is to keep it fixed at the top-right corner of the viewport, so I tried using the fixed position. However, when I do this, the modal turns grey. On the other han ...

Integrate Javascript code into a JavaScript file

Is there a way to incorporate a JavaScript link inside a .js file? Does a .js file support include, require, or import similar to CSS or PHP? Here is the code in question: // Global site tag (gtag.js) - Google Analytics var script = document.createElemen ...

Identify the hover event within a div element

There is a parent div with a child div inside it. I want the border color and text of the child div to turn white when a user hovers over the parent div. For instance, in the example provided, the text inside the box that says No score, yet click and subm ...

Selecting an element using jQuery with the `:eq(i)` selector

I'm attempting to set the height of 10 hi div classes to different values. I am aware of the method to do so. $(".hi:eq(0)").css("height",n[0]); $(".hi:eq(1)").css("height",n[1]); $(".hi:eq(2)").css("height",n[2]); .......... $(".hi:eq(9)").css(" ...

Conceal one element and reveal a different one upon submitting a form

On a web page, I want to hide the form upon submission (either by clicking or pressing enter) and display the results. However, this functionality does not seem to work when the Go web server is running. When I test the HTML file (without executing the Go ...

Troubleshooting: Problems with AJAX POST request in Spring MVC Controller

Here are two different ways to submit form data to a Spring Controller. 1. $("#formData").submit(function(event){ event.preventDefault(); var jqxhr = $.post("submitdata", $("#formData").serialize(), function(data){ ...

Stop the context menu from popping up when the element is right-clicked

Is there a way to create a custom right-click interaction for an element on my website, <div class="myElement"></div>? I want to avoid the default context menu from popping up when the user right-clicks on this element in order to enhance the u ...

In-line Vertical Ticker Display

I attempted to use vTicker, but I found that it does not function properly when used as an inline element. <h1> <div id="vticker"><ul>...</ul></div> Some other headline text </h1> My goal is to have the vertica ...

variances in CSS performance between local and remote hosting

My team and I are all using Internet Explorer 8 on Windows 7 Professional, with Visual Studio 2010 Premium. However, we have noticed that when running our application in localhost mode versus remote mode (on the server), there are differences in the CSS re ...

Create a dynamic animation using Angular to smoothly move a div element across the

I currently have a div with the following content: <div ng-style="{'left': PageMap.ColumnWrap.OverviewPanelLeft + 'px'}"></div> Whenever I press the right key, an event is triggered to change the PageMap.ColumnWrap.Overvie ...

align the horizontal navigation bar

I am facing an issue with my horizontal navigation bar. I want to include a search bar within the navigation bar, but it is causing misalignment. Here is the link to the code: https://jsfiddle.net/r6ntxg2f/ If you look at the menu, you will notice that i ...

Transfer the content from a text area to a div element while keeping the original line breaks

I have a textarea and a div (containing a paragraph tag) where the content of the paragraph tag is updated with the text entered into the textarea upon keyUp. Is there a way to maintain line breaks from the textarea and have them appear as line breaks in ...