"Implementing a Two-State Toggle Button Using the slideToggle Method in JQuery

I'm struggling to implement two different states for the toggle button. Currently, clicking the button reveals the hidden h1 text, but it fails to hide the text when the slideToggle() method is used to retract it. Just to clarify: I want the calendar container to shrink when the toggle button is clicked and display the hidden message in its place, and vice versa.

    
        $(document).on('turbolinks:load', function () {
            $("button").click(function(){
                $("div#hide-calendar").slideToggle("slow", "linear", function(){
                  $("div#calendar-minimized-panel-msg").show();
                });
            });
        });
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


    <div class="col-md-6">
      <div class="close-calendar-btn">
        <button class="btn btn-outline-primary btn-sm" id="hide-calendar">Minimize Calendar</button>
      </div>
      <div class="calendar-title">
        <h4>Appointment Calendar</h4>
      </div>
      <div class="calendar-container" id="hide-calendar">
        <%= month_calendar appointments: @appointments do |date, appointments| %>
            <%= date %>
            <% appointments.each do |appointment| %>
                <div>
                  <%= appointment.name %>
                </div>
            <% end %>
        <% end %>
      </div>
      <div id="calendar-minimized-panel-msg" style= "display: none;">
        <h3>Maximize Calendar Message</h3>
      </div>
    </div>

Answer №1

Ensure the visibility state of an element and adjust the message element state accordingly. Utilize the toggle() method to switch the state and employ is() along with :hidden to determine the boolean value based on the main element's state.

$("button").click(function() {
  $("div#hide-calendar").slideToggle("slow", "linear", function() {
    $("div#calendar-minimized-panel-msg").toggle($(this).is(':hidden'));
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-6">
  <div class="close-calendar-btn">
    <button class="btn btn-outline-primary btn-sm" id="hide-calendar">Minimize Calendar</button>
  </div>
  <div class="calendar-title">
    <h4>Appointment Calendar</h4>
  </div>
  <div class="calendar-container" id="hide-calendar">
    <div>
      fdf
    </div>
  </div>
  <div id="calendar-minimized-panel-msg" style="display: none;">
    <h3>Maximize Calendar Message</h3>
  </div>
</div>

<script type="text/javascript">
  $("button").click(function() {
    $("div#hide-calendar").slideToggle("slow", "linear", function() {
      $("div#calendar-minimized-panel-msg").toggle($(this).is(':hidden'));
    });
  });
</script>

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

Strategies for updating the 'prevState' variable in React.js

I've encountered an array of objects that I need to update the state for. Toggling between the first and second items in the array, the value of isTrue changes from false to true. However, I want the first item to have isTrue: true, and only when I cl ...

Navigational strip within the grid

My grid has a size of 300px. When there are more than 10 records, a vertical scroll bar appears, which is fine. However, at the same time, a horizontal scroll bar also appears, which I do not want to display. My CSS class for vertical scrolling is: .vstyl ...

Ensure that the logo/header remains fixed at the top of the page at all

Currently, I am facing an issue with keeping my logo/header at the top of the page. I have experimented with CSS properties like position: fixed; z-index: 9999; top: 0; left: 0;, and even tried adjusting the positioning to left: 40%; in an attempt to cente ...

Htmlunit driver encounters difficulty executing Javascript

Recently, I switched my Selenium test from using FirefoxDriver to HtmlunitDriver in Java. The test was running smoothly in the Firefox browser but encountered an issue when I made this change: driver = new FirefoxDriver(); to driver = new HtmlUnitDriver ...

Pattern matching for identifying repeated numeric sequences

I'm struggling to come up with a regular expression to identify patterns of repeated numbers (more than twice) such as: 1111 or a1111 or test4555 Can anyone lend a hand with this, please? ...

Challenges with text wrapping in a continuously scrolling container in CSS

I am currently developing a project that requires infinite horizontal scrolling for cells containing thumbnails and text. While most of the functionality is in place, I am facing an issue with text wrapping. The CSS and HTML code are provided below. The fi ...

The AngularJS model is failing to reflect changes in the view when utilizing an input type of number

When I set the input type to number with a specified value, it does not display the value in the input box. However, when I change it to text, it works correctly. <input value="30" ng-model="project.camp_duration" min="30" max="60" id="camp_duration" n ...

Angular back-end data filtering tutorial

I have a user input field where users can search or type in data, but I want to restrict them to only searching for predefined data that is available from the backend. I do not want users to be able to create new data by typing something that is not alread ...

I'm having trouble making out the object items below until I focus more closely

I seem to be having trouble reading the object items below, especially until I reach the 'subscription' section. It seems to be functioning properly but I still need to understand its contents 'subscription [current_period_end]': &apos ...

What are the steps to setting up SystemJS with Auth0?

I am having trouble configuring SystemJS for Auth0 (angular2-jwt) and Angular 2.0.0-beta.6 as I keep encountering the following error message: GET http://localhost:3000/angular2/http 404 (Not Found)fetchTextFromURL @ system.src.js:1068(anonymous function) ...

An error occurred during runtime due to a TypeError when trying to execute the 'setItem' function on 'Storage'. The error message indicates that 2 arguments are required for this function, but only 1 argument

Having an issue with the local storage not working and the screen showing up as white in the browser. 5 | const getFromLocalStorage = () => { 6 | if (typeof window !== undefined) { > 7 | const value = localStorage.getItem(& ...

Using a combination of Internet Explorer, jQuery, Ajax, and XHTML can lead to HTML content being truncated after using either the .html

Trying to encapsulate this complex issue in a single sentence is proving to be quite challenging, so forgive me if I miss the mark. Recently, I launched a website after thorough testing on my local server with all major browsers, including IE8 (in IE8 sta ...

Issue with IPv6 compatibility in Node.js and Selenium causing SocketException due to unavailable protocol family

Encountering this error appears to be unique to when the ios-driver jar is spawned as a Node.js child process. The specific error message is java.net.SocketException: Protocol family unavailable selenium-test.js: var spawn = require('child_process& ...

The login system detected an error due to an incorrect email/username combination

I've implemented a login system on my website that allows users to create an account, log out, and then log back in. However, there seems to be an issue with the error message displaying when someone tries to log into an account that doesn't exis ...

JavaScript does not acknowledge that one variable is greater than or equal to another

My issue lies in the fact that my code recognizes that 100 is less than 2000, but fails to recognize that 200 is less than 1000. Below is my code snippet (I am also using jQuery as a framework): $('.filter-price').submit(function(e) { var ...

Steps to release a react application as an npm package

Hey there! I have a set of JavaScript files named auth.js, cookies.js, hooks.js, product.js, and index.js. My plan is to package them using npm for publishing. In my index.js file, I am exporting all the other files with the syntax: export * from './ ...

Utilizing Flot, JSON, and MySQL for creating a unique date format for the X axis

After hours of struggling to solve a problem using the vast knowledge of the internet, I find myself at a dead end. Can anyone pinpoint the mistake I've been overlooking in my efforts? I'm currently working on generating a graph with Flotr using ...

Synchronize Magento cart totals and items using AJAX

Hello there, I am currently developing a custom AJAX cart for Magento and I prefer not to use any pre-made extensions. I want this cart to be unique and tailored specifically to my needs. Could you please advise me on the best method to retrieve importan ...

Having trouble showing my Google map using canvas

Can anyone help me with this issue that I'm facing? I am working on a JavaScript web page and trying to show a Google map (Google API) along with a canvas tag in the html body. Currently, the map and canvas are only displaying separately. https://i ...

Is there a way to assign a value to the property in AngularJS' ng-options?

There seems to be a common issue that many people (myself included) are facing. When utilizing the ng-options directive within AngularJS to populate the choices for a <select> element, I am struggling to understand how to assign a value to each opti ...