Trouble concealing tab using slideUp function in Jquery

Whenever I click on the 'a' tag, it displays additional HTML content (list) that is controlled by generic JS code for tabs. However, I want to hide the list when I click on the "Title" link again. What can I do to achieve this?

You can view a demo of what I have done so far.

JS

$(".nav_tab>ul>li>a").click(function(event) { 
    $(".nav_tab>ul>li>a").parent().removeClass("activo");
    $(this).parent().toggleClass("activo");

    var capa = $(this).prop('href').split('#');
    $(".nav_tabcontent").slideUp("fast");
    $("#"+capa[1]).slideDown("slow");

    event.preventDefault();
});

$(".nav_tab>ul>li>h3").click(function(event) { 
    $(".nav_tab>ul>li>a").parent().removeClass("activo");

    var o = $(this).parent().find("a");
    o.parent().toggleClass("activo");

    var capa = o.prop('href').split('#');   
    $(".nav_tabcontent").slideUp("fast");
    $("#"+capa[1]).slideToggle("slow");    

    event.preventDefault();
});

Answer №1

To implement the toggle class functionality in jQuery, you just need to use the toggle class method. Here is an example on JSFiddle: http://jsfiddle.net/d90ac70w/2/. Simply check if the div has a class of active.

$(".nav_tab>ul>li>h3").click(function (event) {
    $(".nav_tab>ul>li>a").parent().removeClass("activo");
    var o = $(this).parent().find("a");
    o.parent().toggleClass("activo");

    var capa = o.prop('href').split('#');

    if(!$("#" + capa[1]).hasClass('active')){
      $("#" + capa[1]).slideDown(); 
      $("#" + capa[1]).toggleClass('active'); 
    }else{
      $("#" + capa[1]).slideUp();   
      $("#" + capa[1]).toggleClass('active'); 
    }
    event.preventDefault();
});

As we are using the same jQuery selector repeatedly, we can simplify by chaining the methods together.

$(".nav_tab>ul>li>h3").click(function (event) {
    $(".nav_tab>ul>li>a").parent().removeClass("activo");
    var o = $(this).parent().find("a");
    o.parent().toggleClass("activo");

    var capa = o.prop('href').split('#');

    if(!$("#" + capa[1]).hasClass('active')){
      $("#" + capa[1]).slideDown().toggleClass('active');
    }else{
      $("#" + capa[1]).slideUp().toggleClass('active');
    }

    event.preventDefault();
});

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

Conundrum regarding setting up configuration for express-session middleware in Express version 4.x

Hello, I'm currently diving into node.js and still trying to grasp the concept of configurations in sessions. Below is a basic example of how sessions are used in my app: app.js var express = require('express'); var bodyParser = require(&a ...

Retrieve information from various MongoDB collections

Greetings! I currently have a database with the following collections: db={ "category": [ { "_id": 1, "item": "Cat A", }, { "_id": 2, "item": "Cat B" ...

Passing a JavaScript variable to PHP resulted in the output being displayed as "Array"

After sending a JavaScript variable with the innerHTML "Basic" to PHP via Ajax and then sending an email with that variable, I received "Array" instead of "Basic". This situation has left me puzzled. HTML: <label class="plan-name">Plan name: <b ...

What method can be used to keep Chromium open while using Puppeteer?

I am trying to figure out how to launch only one instance of Chromium from my first script and then connect to it from subsequent scripts. I am aware of the puppeteer.connect() method, but the issue is that when I start the script responsible for launching ...

Creating dynamic animations by shifting the hue of an image on a canvas

Recently, I've been exploring the world of canvas tags and experimenting with drawing images on them. My current project involves creating a fake night/day animation that repeats continuously. After trying various approaches like SVG and CSS3 filters ...

changing tooltip color in bootstrap based on the parent element

Is there a way to adjust the bootstrap tooltip background-color based on the container? For example, my header has a white background and my footer is black. #header .tooltip-inner{ background-color:fade(@grayWhite,80); } #footer .tooltip-inner{ backgro ...

Post-render for HTML linkage

Is there a method to execute customized code after Knockout has inserted the html into the DOM and completed rendering? This is required in order to bind a nested view model to dynamically generated html code. Perhaps like this: <div data-bind="html: ...

Creating unique CSS class and ID names dynamically in PHP

I have two divs with randomly generated IDs and I would like to use those IDs in my CSS instead of using inline styles. Is there a way to achieve this without creating a separate PHP file with a CSS header? For example, I want the padding for the ID $id_ ...

Using jQuery to make checkboxes function like radio buttons, allowing users the ability to uncheck all options

Within my form, I have a specific set of checkboxes that need to behave like radio buttons. However, they must remain checkboxes for consistency with the rest of the form. The issue arises when a user wants to uncheck all selections after making a choice. ...

Make the HTML element expand to the remaining height of the viewport

Need help with creating a section that automatically fills the remaining viewport height below the navigation bar. The section includes a centered div and background image, along with a button at the bottom for scrolling down by one full viewport height. H ...

Is there a feature similar to Google/YouTube Insight called "Interactive PNGs"?

Recently, I have been exploring the YouTube Insight feature and I am intrigued by how their chart PNGs are generated. When you view the statistics for a video, the information is presented in interactive PNG images. These images seem to be entirely compos ...

Guide on Updating the ColModel Dynamically in JAVASCRIPT/HTML using clearGridData, setGridParam, and reloadGrid Functions

I am trying to figure out how to dynamically change the colmodel of my grid using a function that is called by a SELECT. The reason for this is because my grid has different periods and needs to display either cost or tons based on user selection. Below i ...

"Enhance your Vue components with a directive to customize or adjust element attributes

I have a variety of elements structured like this: <some-custom-component :errors="formErrors.has(getErrorKey('town'))" :error-messages="formErrors.getAll(getErrorKey('town'))" ></some-custom-component> The formErrors ...

"Unlock the Power of VueJS 2 with Dynamic Templates

Just starting out as a VueJS student! I created a "MainTemplate.vue", which includes a menu, footer, header... Then I decided to create another .vue file named "ComponentB.vue". Here is the content of my ComponentB.vue file: <template> <h ...

Conceal a different div unless it includes

Hi everyone, I need help with a filter code snippet: $(".title").not(":contains('" + $("[name=filter]").val() + "')").hide() The issue I'm facing is that the .title class is nested within the .sortAll class along with many other divs. I w ...

The attempt to run 'setProperty' on 'CSSStyleDeclaration' was unsuccessful as these styles are precalculated, rendering the 'opacity' property unchangeable

I am attempting to change the value of a property in my pseudo element CSS class using a JavaScript file. Unfortunately, I keep encountering the error mentioned in the title. Is there any other method that can be used to achieve this? CSS Code: .list { ...

Creating an HTML form that spans across multiple pages: Tips and tricks

When creating a shopping form that includes items like mugs and tshirts, is it permissible according to website standards to design a form that spans multiple pages? One option could be to add radio buttons for choosing a color directly on the main form ...

retrieve the webpage hosted on the server

Seeking to develop a website where users can input a website URL and have it loaded from the server hosting the HTML page, rather than from the user's computer as iframe does by default. I've experimented with various techniques but all seem to l ...

The AJAX response consists of HTML content from the page rather than just a singular value

I have a 'like' button set up using PHP/MySQL, AJAX and jQuery to increase likes by +1 each time it is clicked. The functionality works correctly and updates the database with the new value as expected. However, after the AJAX call, I am receivin ...

"div p" or "div * p"? Do they have the same meaning or is there a difference between them?

My knowledge of the "grandchild" selector in CSS was limited until I came across this insightful article. Intrigued, I decided to experiment with it and found that it resembles the universal selector (which targets all elements). Let's take a look at ...