Checkbox selection causing Bootstrap accordion to collapse

Hey everyone, I'm currently working with Bootstrap 5 accordion and facing an issue where the input checkbox is triggering the "collapse" event of the accordion. I attempted to relocate the checkbox outside the scope of the accordion button but that solution didn't seem ideal.

My setup involves using Svelte to render each accordion item dynamically with data fetched from the backend.

Here's a snippet of my current code:

<div class="container">
        <div class="row">
            <div class="col-md-10 col-10 col-sm-10 responsiveFix">
                <div class="accordion" id="accordionObjective">
                    {#each objectives as objective}
                        <div class="accordion-item">
                            <h2
                                class="accordion-header"
                                id={"heading" + objective.objectiveId}
                            >
                                <button
                                    class="accordion-button objectiveTitle collapsed"
                                    type="button"
                                    data-bs-toggle="collapse"
                                    data-bs-target={"#collapse" +
                                        objective.objectiveId}
                                    aria-expanded="false"
                                    aria-controls={"collapse" +
                                        objective.objectiveId}
                                >
                                    <div class="form-check">
                                        <input
                                            class="form-check-input"
                                            type="checkbox"
                                            
                                            value={objective.objectiveId}
                                            id="myCollapse"
                                            checked={objective.value}
                                            on:click={postObjective(
                                                objective.objectiveId,
                                                objective.value
                                            )}
                                        />
                                    </div>
                                    <span> {objective.title}</span>
                                </button>
                            </h2>
                            <div
                                id={"collapse" + objective.objectiveId}
                                class="accordion-collapse collapse"
                                aria-labelledby={"heading" +
                                    objective.objectiveId}
                                data-bs-parent="#accordionObjective"
                            >
                                <div class="accordion-body">
                                    <span>{objective.description}</span>
                                </div>
                            </div>
                        </div>
                    {/each}
                </div>
            </div>
        </div>
    </div>

The result of this code can be viewed here.

Additionally, here is a screenshot illustrating the issue I encounter after clicking on any checkbox: https://i.sstatic.net/2VyDn.png

I've also experimented with binding a bootstrap 5 function to the checkbox input without success.

If anyone has any insights or suggestions for resolving this problem, I would greatly appreciate it. Thank you!

Answer №1

I don't have much experience with Bootstrap 5, but after taking a quick look at your code, I noticed the following:

<button on:click={postObjective(objective.objectiveId, objective.value )}>

This function is run during render time, and its return value is added as an event listener for your on:click

The accurate syntax should be

<button on:click={() => postObjective(....)}>

As it stands now, the code might not produce the intended outcome.

Edit:

In this scenario, the input is nested within a button, which means clicking the input will also trigger the button click. To prevent this, you need to stop the event from bubbling up. Luckily, Svelte offers a useful helper for this:

<button on:click|stopPropagation={() => postObjective(....)}>

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

Utilizing Semantic-UI-React and Webpack to Set the Path for an Image

I am currently developing a ReactJS application using webpack. This basic example is supposed to display an <img> tag with a specified URL in the src attribute. How can I bundle the image resource using webpack and process it with the appropriate l ...

JavaScript - Attempting to retrieve data using AJAX

Struggling with extracting data from an AJAX call using jQuery while implementing RequireJS for better maintainability and modularity in my JavaScript. Still new to RequireJS so not entirely sure if I'm on the right track. Just aiming to keep my JS mo ...

Storing values from a content script into textboxes using a button press: a simple guide

I am new to creating chrome extensions and currently utilizing a content script to fetch values. However, I am facing difficulty in loading these values into the popup.html. Here is the code snippet: popup.html <head> <script src ...

Select the text inside the current cell of a jqGrid table

The issue at hand is that when using jqGrid with cellEdit:true, users are unable to select text in a cell. Once the mouse button is released, the selection resets. This limitation prevents users from copying the selected text within cells. Has anyone enco ...

Learn the process of transferring data from a page to a layout in NUXT

I am in the process of creating a basic website. The goal is to dynamically change the Navbar elements based on the data set in the navLayout within the page template. My plan is to pass this data to the layout and then utilize props to transmit it to the ...

NodeJS error message: "The callback provided is not a function

As someone new to the world of NodeJS, I'm facing challenges in understanding how to pass variables and objects between functions. Any help on what I might be doing wrong would be greatly appreciated. Let's take a look at this code snippet: Inc ...

Is the Vuex mutation properly formatted?

Is the mutation method correctly written to modify the initial state array? I'm uncertain about the last few lines of the mutation method. What am I missing, if anything? // Storing state: { flights: [ {trip_class: 0, number_of_change="1"}, ...

The cookie generated through jQuery(view) cannot be retrieved in PHP(controller) on the initial try

I've encountered a strange issue with Chrome and Firefox, surprisingly it works fine on IE occasionally. [Edit1: Problem occurs intermittently with IE as well] Premise: On my homepageView.php, I utilize jQuery to create a cookie, which I'll re ...

I am looking to dynamically insert a text field into an HTML form using jQuery or JavaScript when a specific button is clicked

This is my code snippet: <div class="rButtons"> <input type="radio" name="numbers" value="10" />10 <input type="radio" name="numbers" value="20" />20 <input type="radio" name="numbers" value="other" />other </div> ...

Ways to tally elements that have been dynamically loaded onto the webpage through AJAX requests

My page has a dynamic region that is continuously updated using jQuery's .ajax and .load methods. In order to submit the form, there need to be at least 3 of these 'regions', so I have to keep track of the number of DIVs with a specific cla ...

What is the process of making circle or square shapes with text inside using React, without the use of SVG images?

Is there a way to create circle and square shapes in React with custom text inside, without relying on SVG images? Check out this example: https://i.sstatic.net/iHZgy.png I attempted the code below but it did not render any shapes: import React from &apos ...

React and Redux Toolkit collaborated to create a seamless shared state management system,

Currently, I am developing a simple application to experiment with Redux Toolkit alongside React. Despite being able to view the object in the Redux Chrome tab, I am facing difficulties accessing it using React hooks within components. The code for my sli ...

Understanding the behavior of Bootstrap input-groups when containing hidden sub elements

In a scenario where a button and an input field are enclosed within a div with a bootstrap class named input-group, they expand to occupy the entire width of the enclosing element. However, if the button is hidden, the input field unexpectedly contracts to ...

Place the Material-UI Drawer component beneath the Appbar in the layout

Currently, I am working on developing a single-page application using Material-UI. In this project, I have integrated the use of an AppBar along with a ToolBar and a Drawer. However, I have encountered an issue where the Drawer is overlapping the AppBar an ...

Displaying various Vue components within Laravel 6.x

After diving into Laravel and Vue, I managed to put together a navigation component as well as an article component. The problem I'm facing is that although my navigation vue component is visible, the article component seems to be missing. Reviewing ...

Exploring the possibilities of event-driven programming with Java and Javascript?

When performing computations on a server, the client inputs data that is captured through Javascript. A XMLHttpRequest is made to send this data to the server for processing. What happens if the computation takes an hour and the client leaves or switches o ...

What's preventing me from drawing a line on this D3.js chart with my Angular code?

I am attempting to create a graph on my webpage using the D3.js library for JavaScript. I am able to draw a line on an SVG using HTML, but when I try to draw a line using JavaScript, I encounter the error message: ReferenceError: d3 is not defined. Even t ...

Tips on entering a text field that automatically fills in using Python Selenium

One of the challenges I am facing on my website is an address input text field that gets automatically populated using javascript. Unlike a drop-down field where you can select values or a standard text field where you can manually type in information, thi ...

Choose the element before and add a style effect with CSS

I'm trying to create a layout with 3 <div> elements in one horizontal line, each with a width of 33.33%. When hovering over one div, I want its width to expand to 50% while the other two shrink to 25%. <div id="A"></div> <div id= ...

Is there a way to convert an array into a single comma-separated value using parameters?

Is there a way to parameterize an array as a single name-value pair that is comma separated? I have tried using jQuery $.param, but it creates a parameter for each value in the array instead of just one. Unfortunately, it seems that there is no option or s ...