The jQuery toggle functionality seems to be malfunctioning

I have created a form that should toggle (hide and show) with the click of a button, but for some reason it's not working. Can someone please take a look at my code below and let me know what I'm doing wrong?

$(document).ready(function () {
        $("#add_facility").click(function () {
            $("#facility-form").toggle('slow');
        });
    });
.facility-form {
        background-color: #e3edfa;
        padding: 4px;
        cursor: initial;
        display: none;
    }
 <button class="btn" id="add_facility">
    <i class="fa fa-plus" aria-hidden="true"></i> 
    Add Facilities
</button>
<div class="facility-form">
    <form id="facility-form1">
        <div class="row">
            <div class="col-md-4">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" value="">Internet
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" value="">Bar
                    </label>
                </div>
            </div>
            <div class="col-md-4">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" value="">Free Wifi
                    </label>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" value="">Spa
                    </label>
                </div>
           </div>
        </div>
    </form>
</div>

   

Answer №1

Instead of selecting by id, make sure to target the correct element with a class in your form; check out the following code snippet for reference:

$(document).ready(function() {
  $("#add_facility").click(function() {
    $(".facility-form").toggle('slow');   // ***
  });
});
.facility-form {
  background-color: #e3edfa;
  padding: 4px;
  cursor: initial;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button class="btn" id="add_facility">
  <i class="fa fa-plus" aria-hidden="true"></i> Add Facilities
</button>
<div class="facility-form">
  <form id="facility-form1">
    <div class="row">
      <div class="col-md-4">
        <div class="checkbox">
          <label>
            <input type="checkbox" value="">Internet
          </label>
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox" value="">Wifi
          </label>
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox" value="">Bar
          </label>
        </div>
      </div>
      <div class="col-md-4">
        <div class="checkbox">
          <label>
            <input type="checkbox" value="">Free Wifi
          </label>
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox" value="">Spa
          </label>
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox" value="">Parking
          </label>
        </div>
      </div>
      <div class="col-md-4">
        <div class="checkbox">
          <label>
            <input type="checkbox" value="">Indoor Pool
          </label>
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox" value="">Family Rooms
          </label>
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox" value="">Smoking Rooms
          </label>
        </div>
      </div>
    </div>
  </form>
</div>

Answer №2

It's important to utilize the class selector instead of an ID selector

Replace this line with,

    $("#facility-form").toggle('slow');

To

    $(".facility-form").toggle('slow');

Check out this functional Fiddle : https://jsfiddle.net/pz6h4g7q/

I hope this resolves your issue!

Answer №3

this isn't right

$("#facility-form").toggle('slow');

update to

$(".facility-form").toggle('slow');

Answer №4

Revise this:

$("#facility-form").toggle('slow');

to

$(".facility-form").toggle('slow');

Remember, facility-form is a class, not an ID.

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

Plugin using jQuery that adds a UserVoice-inspired feedback tab to any webpage

Does anyone know of a jQuery plug-in that can replicate the fixed position tabs often seen on websites, where the tab is affixed to either the left or right side of the webpage? If you're looking for an example, UserVoice offers a script to add a fee ...

iOS users are experiencing issues with mobile responsiveness

I've encountered a strange issue with the mobile responsive design of a website. The website in question is While everything displays correctly on desktop using dev tools > responsive, and on any Android device, the problem arises when viewing i ...

It is not possible to submit two forms at once with only one button click without relying on JQuery

I need to figure out a way to submit two forms using a single button in next.js without relying on document.getElementById. The approach I've taken involves having two form tags and then capturing their data in two separate objects. My goal is to hav ...

Steps for adding a forked version of a library in package.json

Recently, I came across a React JS library called React Pacomo. Since the original version of this library is no longer being maintained, I decided to use my own forked version for my project. However, I am facing issues with compiling or building the libr ...

What could be the reason for the defaultCommandTimeout not functioning as expected in my script

Is there a way to wait for only one particular element in Cypress without having to add wait commands everywhere in the test framework? I've come across the solution of adding defaultCommandTimeout in the cypress.json file, but I don't want it t ...

Customizing the hover color of text in Bootstrap 5

I'm looking to modify the hover color on my navbar. The Navbar currently has a black background with text in #b3b3b3 color. I want the text to turn white when hovered over, instead of staying the same color, but none of the CSS codes I've tried s ...

Tips for sending and retrieving parameters using the POST technique

Currently, I am in the process of building a user authentication form for my website using Javascript. I am utilizing Vue JS on the client-side and NodeJS with ExpressJS on the server-side. For the server-side functionality, I have implemented the followi ...

Using Jquery to add elements one by one

Here is the Code for my project: Displayed below is the content of my CSV file: Item, Quantity, Price; LED, 100, $10; PIR, 1, $5; DS18B20, 10, $5; This segment showcases the jquery file I've composed: $(document).ready(function() { $.ajax({ ...

Choose the current parent item using Angular's ng-selected and $index

http://plnkr.co/edit/fwwAd4bn6z2vxVN2FUL7?p=preview On the Plunker page linked above, I am trying to achieve a specific functionality. I would like the 3 dropdown lists to display values A, B, and C initially. When a 4th dropdown option is added, it shoul ...

Error: The terminal reports that the property 'then' cannot be found on the data type 'false' while trying to compile an Angular application

In my Angular application, which I initiate through the terminal with the command ng serve, I am encountering build errors that are showing in red on the terminal screen. ✔ Compiled successfully. ⠋ Generating browser application bundles... Error: s ...

Iterate over div elements using jQuery

I am experimenting with creating a jQuery slider using divs: <div id="block-1"> <div id="bannerleft"> <div class="wsite-header-1"></div> </div> <div id="bannerright"> < ...

Using jQuery to toggle the visibility of a group of radio buttons causes the div to quickly appear and disappear

My code is working perfectly as it shows up when needed and disappears when necessary. The concept behind this form is that the user can check a box, view more required form data related to the checked item, make those fields required, and then uncheck the ...

Wait for the scope value to become available before executing the directive or div

I have a unique directive created for SoundCloud that necessitates the SoundCloud URL. The URL is fetched from the database using the $http service, but the issue arises when the div for the directive is loaded before the URL value is defined. The code fo ...

I need to update my database by sending requests to my API, as the changes are not being reflected in the current database state. I am eager to see the

Struggling to grasp the concept of making requests to an API that uses express for CRUD operations. In the code snippet below, .get(/bears) displays a form and in app.post('/bears'), I'm using the 'request' module to send a request ...

Issue detected in FullCalendar: date.getFullYear is not functioning as expected

I am working on adding a line of content to each cell in my calendar, specifically displaying the date in Hebrew next to the regular date number. Currently, I am testing with the standard sample calendar. The code snippet I am using is as follows: dayRe ...

Issue with updating nested child object reference in Redux state input value

I have a function in redux that updates an object with a specified path and value. The inputs on my page are based on the values in the object stored in state. Whenever the listingObj is modified in redux, I want the DOM to automatically refresh. This i ...

In the realm of JavaScript, what happens when a function yields yet another function while also welcoming another function as an argument?

After following a Node & Express project tutorial on YouTube, I encountered the following code snippet in an async JavaScript file: const asyncHWrapper = (fn) => { return async (req, res, next) => { try { await fn(req, res, next); } c ...

Universal PM2 Configuration Settings in JSON Format

My current process involves initiating numerous Node.js processes using pm2. These processes are configured in a JSON file and started by executing pm2 start pm2.json Upon examining the JSON file provided below, it's evident that there is significan ...

What is the best way to navigate between different areas of an image using html and javascript?

I am currently in the process of learning how to develop mobile applications, and I am still in the early stages. Although this question is not directly related to mobile development, it pertains more to html/css/js. My goal is to create a simple game wh ...

Unable to display shadows in Three.js

I've hit a roadblock with the shadow effect at the moment, despite trying various solutions for hours, it's still not appearing. Can anyone point out what I may have done incorrectly? Below is the code snippet for my current scene: //Setting up ...