Using Jquery to toggle classes between "display block" and "display none

Hello friends, I'm in need of some help with my code.

$(document).ready(function() {
     var circle = $('.circle');
     $(".send a").click(function(e) {
      e.preventDefault();
     $('.wrap').css('display', 'block');

        if (circle.hasClass('bounceInLeft')) {
         circle.removeClass('bounceInLeft').addClass('bounceOutRight');

          }
           else 
          {

         $('.circle').addClass('animated bounceOutRight');
         circle.removeClass('bounceOutRight').addClass('bounceInLeft');
           }

      });

  });

If you click on .send a, then .wrap will become visible by having its display set to 'block'. How can I make it so that when you click on .send a for a second time, .wrap goes back to being hidden (display: none)? I know the code should look something like this:

$('.wrap').css('display', 'none');
but I'm not sure where exactly to include this piece of code...

.wrap {
  max-width: 400px;
  margin: 4em auto;
  text-align: center;
  display:none;
  background-color:#fff;
}

Answer №1

To change the visibility of the element with each click, you can utilize the .toggle() method.

$(".send a").on('click', function(e) {
    $('.wrap').toggle();
    // ..
});

Another option is to use .toggleClass(), and incorporate a class like .hide:

.hide {
    display: none; /* To increase specificity if needed */
}
$(".send a").on('click', function(e) {
    $('.wrap').toggleClass('hide');
    // ..
});

If you prefer manually adding/removing inline CSS, consider using a conditional statement like this:

$('.wrap').is(':hidden') ? $('.wrap').show() : $('.wrap').hide();

Answer №2

Example

I implemented a variable that tracks the number of clicks on the anchor element.

This method is straightforward and I trust it will be effective for you.
Best wishes!

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

Make the div come to life by animating it as its

I'm currently working on a project involving three boxes that change position when clicked. I want to be able to switch the selected box with another box, but I am struggling to add an animation to show the transition between them. I would greatly ap ...

Enabling CORS Allow-Access-Control-Origin for Chrome on Mobile Devices

I'm encountering a mobile error while testing my new responsive web app on Chrome. I've tried various methods like adding PHP headers and passing headers in AJAX requests. On the desktop, using CORS toggle extension in Chrome helped resolve the i ...

Use jQuery to update information in a table based on a unique identifier

In my Laravel project, I have a table where data needs to be updated in a specific row without reloading the page using jQuery. The data is supposed to be updated in the row: .... <tbody> @foreach ($tasks as $task) <tr> <td&g ...

Using jQuery to handle multiple buttons with the same ID

Is there a way to address the issue of multiple buttons sharing the same id? Currently, when clicking on any button, it only updates the record with id=1. How can this problem be resolved? div id="ShowPrace"> <?php try { $stmt = $pdo->prepare(" ...

The primary objective of utilizing margin

Just embarked on my journey to mastering HTML and CSS. Regarding the 'margin' property: Does its primary function revolve around centering elements horizontally on a webpage? I've crafted a navigation menu for a website, yet I struggle to ...

Button to close Jquery Dialog

I've set up a custom alert UI using jQuery UI, but I'm having trouble getting the close button to work properly. Here's my code snippet where I'm trying to override the default alert() function with jQuery UI dialog as described in this ...

Using JQuery to assign a class to every third item in a list

How can I add a class name to every third list item using addClass() instead of css()? I tried the following: $('ul li:nth-child(3n)').addClass('grey'); However, it doesn't seem to be working. What did I do wrong? Here is my co ...

Error: The global variable cannot be emptied due to an issue with the Ajax request

As someone who is relatively new to the world of Javascript/jquery and async, I have spent a significant amount of time reading through various forums. Unfortunately, I have yet to come across a solution that addresses my specific issue. The problem at ha ...

JavaScript CompleteLink message

Hey there, I've come across a JavaScript code for a countdown timer but need some assistance with it. The issue I'm facing is that within the code, there's a line that displays a message to the user when the timer reaches zero. I want to kn ...

Is there a way to stop the continuous loading of AJAX requests?

For weeks, I have been facing a persistent issue that I've attempted to solve in various ways. The problem lies in my ajax search function. Every time I initiate a search, it continues loading multiple times. Surprisingly, the more searches I perform ...

Is there a way to align these buttons in the middle?

I am currently designing a webpage at The challenge I'm facing is: How can I center those buttons on the page? They need to remain centered and responsive even when the page is resized. Additionally, the spacing between the buttons is inconsistent. ...

Showing a group of users in real-time as they connect using Socket IO

I've been working on setting up a system in Socket IO to create a list of individuals who join a 'party room'. The plan is to lock the party room once all players are present, and then display views to users. However, I've hit a roadblo ...

What could be the reason for the lower section of my website not being displayed?

I'm having an issue with a new div I added to the bottom half of my website - it's not showing up. Oddly enough, when I test the same code on a separate web page, it works fine. Can someone please take a look at the code and help me out? Thank yo ...

What is the optimal method for transmitting data for a substantially large music playlist via HTTP?

I am currently in the process of developing an online music player. My main challenge lies in retrieving a comprehensive list of songs from the database and transmitting it to the user. The user should have the ability to create playlists on-the-go, hence ...

Is there a way to manipulate the src value using jQuery or JavaScript?

var fileref = document.createElement('script'); fileref.setAttribute("type","text/javascript"); fileref.setAttribute("src", "http://search.twitter.com/search.json? q="+buildString+"&callback=TweetTick&rpp=50"); ...

Try repeating the Ajax request until it is successful

Is there a way to continuously repeat an Ajax request until it returns 1, and stop if it returns 0? while(1){ $.ajax({ type: "POST", url: '/admin/importdata/', data: $info, dataType: "json", success: function($result) { ...

Ensure that the next iteration of .each() does not begin until all nested functions have finished executing

Is there a way to ensure that my .each() function waits for all nested functions to complete before moving on? Here is the code I am working with: HTML: <div> <p>Text 1</p> <p>Text 2</p> <p>Text 3</p> & ...

What can I do to adjust the Navigation Item and Dropdown placement?

I've been working with Bootstrap dropdowns, but they're not aligning correctly for some reason. I've tried multiple solutions like updating my Bootstrap versions, but none of them seem to be working. Here is the code: <script src=" ...

maintaining the alignment of one div's right boundary with another div's right boundary

---------------------- ----------------------------->edge X | | | | | logo | | dropdown menu ...

Mastering jQuery UI: A beginner's guide to utilizing the date picker widget

This is my first venture into the world of HTML and JavaScript, so I am asking for your patience. I am trying to incorporate the Datepicker widget from jQuery UI, following the instructions on the Getting Started page. There seems to be an issue with lin ...