Toggle visibility of an element when clicked

I have recently started learning about jquery. In my code, I have two p tags with classes .angle_up and .angle_down respectively.

The angle_up class initially has the active class assigned to it. When a user clicks on .angle_down, I want to remove the .active class from angle_up and apply it to .angle_down.

I want to display the element with the class ele with an animated effect when the active class is present. Essentially, I want to achieve a toggle effect. Any ideas on how I can accomplish this? Thank you in advance.

$(document).on("click", ".angle_up", function () {
        $(this).removeClass("active");
        var angledwn = $('.angle_down');
        angledwn.addClass("active");
        var ele = $('.ele');
        ele.toggle('5000');
    });
.angle_up, .angle_down{
    color: #0096C9;
    display: none;
}
.angle_up.active, .angle_down.active{
    color: #0096C9;
    cursor: pointer;
    display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="angle up active">First</p>
<p class="angle_down"> Second</p>
<p class="ele">To hide or display</p>

Answer №1

There is a small error in your html code. The class should be angle_up instead of angle.up.

To fix this, add a common class .angle to both .angle_up and .angle_down elements and use that class as a selector for your click event.

$(document).on("click", ".angle", function () {
        var angledwn = $('.angle').not('.active');
        $(this).removeClass("active");
        angledwn.addClass("active");
        var ele = $('.ele');
        ele.toggle('5000');
    });
.angle_up, .angle_down{
    color: #0096C9;
    display: none;
}
.angle_up.active, .angle_down.active{
    color: #0096C9;
    cursor: pointer;
    display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="angle angle_up active">First</p>
<p class="angle angle_down"> Second</p>
<p class="ele">To hide to display</p>

Answer №2

To achieve this, you don't have to select the element by class. Simply select by the tag p and use .not() to add a class to the P element that was not clicked.

$(document).on("click", "p", function () {
        $(this).removeClass("active");
        $("p").not(this).addClass("active");
        var ele = $('.ele');
        ele.toggle('5000');
    });
.angle_up, .angle_down{
    color: #0096C9;
    display: none;
}
.angle_up.active, .angle_down.active{
    color: #0096C9;
    cursor: pointer;
    display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="angle_up active">First</p>

<p class="angle_down"> Second</p>



<p class="ele">To hide to display</p>

P.S :

<p class="angle up active">First</p>

you need to correct it, it should be

 <p class="angle_up active">First</p> 

you are missing _ in class name

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

Execute a Bash script using Node.js based on a request from the client

I'm trying to find a way to execute a server-side script when a user clicks a button in the browser... After doing some research, I still can't seem to figure it out. Here's what we have: A Node.js server running on Fedora Red Hat (on lo ...

Implementing a jQuery UI tooltip for toggling functionality

Currently, there seems to be an issue with the tooltip not displaying on the page. It only appears when clicking on the toggle. How can I modify this so that the tooltip shows without the need to click on the toggle...? Below is the code snippet: Javascr ...

Is there a way to delete the header box in IPBoard?

Recently, one of our team members working on the website decided to go rogue and sneak in a large box featuring a Dremel ad. Unfortunately, I am clueless when it comes to removing it. The website is self-hosted with full admin access. ...

"Can anyone provide guidance on how to use Twig to read from a JSON file

I currently have a basic PHP file set up as follows: <?php // Loading our autoloader require_once __DIR__.'/vendor/autoload.php'; // Setting the location of our Twig templates $loader = new Twig_Loader_Filesystem(__DIR__.'/views& ...

Animating a CSS shape with the .animate method

I need help creating a dynamic graphic for my school project using css, jquery, and html. I want to make a rectangle that moves across the screen, but I'm having trouble getting it to work properly. Despite trying different variations of the animate f ...

Guide to utilizing element reference with material UI components in Angular

I am facing difficulty in accessing the reference of an element. My intention is to wrap the material UI elements within a div and set the opacity: 0 so that it remains hidden in the HTML, allowing me to handle the value in my component.ts file. The main g ...

Conceal the information beneath a see-through fixed navigation bar when scrolling downward

the issue: I am facing a challenge with my transparent fixed navbar that has a margin-top gap. The content below the navbar needs to be positioned under it while scrolling down, but the background of the page is a dynamic slideshow of various images. This ...

Activating a inaccessible element

Attempting to enable a disabled element upon clicking a P element, the following code snippet demonstrates storing a value from one textbox into another. The appended div contains a subsequent textbox which will be disabled. On mouseover of the div, option ...

What causes the position: sticky; property to fail in React implementations?

Currently, I am facing a challenge in making two sidebars sticky so that they will follow as the user scrolls until they reach the bottom of the page. In my current editing project, this implementation seems to be more complex compared to other programs w ...

Utilizing Font Awesome icons within a JSON structure

I'm running into a bit of trouble trying to display font awesome icons. The text is written in json using a rakefile, and I'm attempting to insert a font awesome icon within the text. However, I'm facing difficulties because the text is in j ...

What is the best way to send the accurate data type from PHP to Javascript?

My approach involves using the jQuery post method to insert a record in MySQL. The issue I'm facing is that when comparing the output of the PHP using ==, all three conditionals function correctly. However, with ===, the first two conditionals return ...

Ways to eliminate the white background placed behind the arrow on my bootstrap carousel

I've been attempting to create a video carousel using Bootstrap 5, and one issue I'm encountering is the appearance of a white background when hovering over the arrow. Normally, it shouldn't display a background, but for some reason, it does ...

"Revolutionize your time input with Angular's X-editable timepicker or a

Currently, I am utilizing angular x-editable (Editable row) within my Laravel project. My goal is to incorporate a timepicker with some additional configuration, as the x-editable component does not natively support time functionalities. Additionally, I ne ...

JavaScript- Tabbed Navigation with Lists as the Content

Currently, I am facing a frustrating issue in finding a suitable solution. My website uses tabs that utilize the UL, LI system, similar to most tab systems found in tutorials. The problem arises because the javascript on my site interferes with using the ...

Incorporate the HTML CTA on top of the background image within the email template

In my email template, I am trying to place an HTML CTA over a background image. The code works perfectly on all email clients including mobile and desktop, except for Outlook where the alignment of the CTA copy is off. Below is the code snippet along with ...

Build a table with consistent columns and flexible rows utilizing CSS and HTML

Just starting out with CSS/HTML and looking for guidance on creating a table with a variable number of rows. Any tips on how to achieve this using CSS? I'm aiming to replicate a table with a similar structure, but struggling to figure out how to defin ...

Activate the 'click' function for changing data sources

My webpage has a unique feature where checkboxes are generated dynamically from a .csv file. These checkboxes include an additional 'Select All' option created dynamically as well. The functionality I'm aiming for is that upon checking the & ...

The web form response fails to show any content beyond a space character when viewed on the console

I'm facing a new issue and need some assistance to resolve it. I have successfully extracted information from a form and stored it in a database. However, I've noticed that when I display the data on the console, parts of the information get cut ...

Achieving centered text alignment in a span using Bootstrap 5

I am facing an issue with a span in an input group that has a minimum width of 6rem. This is causing the text to not be centered, as spans typically adjust to the width of the text itself. .input-group-text { min-width: 6rem; } <link rel="stylesheet ...

Customize the focus function for an individual element

I am working on a custom component that needs to seamlessly integrate with the native blur and focus functions. My goal is to override these functions in order to achieve the specific functionality I need. Currently, I have managed to override the prototy ...