Unable to make anchor tag inside button effectively collapse another div

My Nuxt 2 SSR + Bootstrap 5 application includes the following code snippet:

    <button 
        v-for="file of orderProduct.files"
        class="collapsed son-collapse" 
        type="button" 
        data-bs-toggle="collapse"
        :data-bs-target="`#collapseField${orderProduct.id}`"
        aria-expanded="false"
        :aria-controls="`collapseField${orderProduct.id}`"
    >
                            
        <p><i class="fa fa-file-alt"></i> {{ file.original_filename }}</p>
        <div>
            <a 
                @click.stop
                :href="`http://example.com`"
                target="_blank"
            >
                <i class="far fa-eye"></i>View
            </a>
            <span>{{ file.page_count }} {{ $tc('home.pagina', file.page_count) }}</span>
        </div>
    </button>
    <div 
        :id="`collapseField${orderProduct.id}`"
        class="collapse summary cost-ajax" 
    >  
       ...
    </div>

Although Bootstrap's JS Collapse component toggles the corresponding div when the button is clicked, I am facing an issue where a tab should open upon clicking the anchor tag.

The tab does not open as expected. Instead, the div is toggled because the click event on the anchor propagates into the button. I have attempted to address this by using Vue.js's built-in event modifier @click.stop and even tried calling e.stopPropagation() within a method, but these solutions have not corrected the behavior.

Interestingly, clicking on the i tag inside the anchor opens the tab, while the collapsible element still toggles. This behavior adds to the complexity of the issue.

Answer №1

Instead of enclosing both the button and anchor tag within the same container, it is advisable to separate them. Placing the anchor tag outside of the button ensures that clicking on the anchor does not interfere with the button's functionality.

<button
  v-for="file of orderProduct.files"
  class="collapsed son-collapse"
  type="button"
  data-bs-toggle="collapse"
  :data-bs-target="`#collapseField${orderProduct.id}`"
  aria-expanded="false"
  :aria-controls="`collapseField${orderProduct.id}`"
>
  <p><i class="fa fa-file-alt"></i> {{ file.original_filename }}</p>
  <div>
    {{ file.page_count }} {{ $tc('home.pagina', file.page_count) }}
  </div>
</button>
<a
  @click.stop
  :href="`http://example.com`"
  target="_blank"
>
  <i class="far fa-eye"></i>View
</a> 

Furthermore, Separate Click Handlers: By separating the button and anchor elements, you can handle the click events individually for each element. Assign the data-bs-toggle attribute exclusively to the button element to regulate the collapse behavior. The anchor tag can have its own click handler to open a new tab.

<button
  v-for="file of orderProduct.files"
  class="collapsed son-collapse"
  type="button"
  data-bs-toggle="collapse"
  :data-bs-target="`#collapseField${orderProduct.id}`"
  aria-expanded="false"
  :aria-controls="`collapseField${orderProduct.id}`"
>
  <p><i class="fa fa-file-alt"></i> {{ file.original_filename }}</p>
  <div>
    {{ file.page_count }} {{ $tc('home.pagina', file.page_count) }}
  </div>
</button>
<a
  @click.prevent
  :href="`http://example.com`"
  target="_blank"
>
  <i class="far fa-eye"></i>View
</a>

Note that @click.prevent is used on the anchor tag to prevent the default behavior and successfully open the link in a new tab without impacting the collapsing function of the button.

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

Guide to excluding all subdependencies using webpack-node-externals

My current setup involves using webpack to bundle both server assets and client code by specifying the target property. While this configuration has been working well, I encountered an issue where webpack includes all modules from node_modules even for ser ...

Using Vue.js and JavaScript to access a single variable across multiple class methods

I am working on organizing my custom channel logic into its own class. During the created lifecycle method, I am executing each method in the class I created. However, I am facing a challenge in retaining the instance of the socket that was created in the ...

Find all the different ways that substrings can be combined in an array

If I have a string input such as "auto encoder" and an array of strings const arr = ['autoencoder', 'auto-encoder', 'autoencoder'] I am looking to find a way for the input string to match with all three values in the array. ...

A collection of items that mysteriously affix themselves to the top of the page as

Unique Context In my webpage, I have a specific div element with the CSS property of overflow: auto. Within this scrolling div, there is structured content like this: <h3>Group A</h3> <ul> <li>Item 1</li> <li>I ...

Where should the logic for the Redux app be implemented for optimal performance?

My react-redux app features an action-creator that filters a list of objects based on a specified title: export const filterItems = (title) => dispatch => { axios.get(`/api/items/?title=${title}`) .then(res => { dispatch({ ...

Positioning an Element on a Web Page in Real-Time

Trying to implement an Emoji picker in my React application, I have two toggle buttons on the page to show the picker. I want it to appear where the Toggle button is clicked - whether at the top or bottom of the page. The challenge is to ensure that the pi ...

A step-by-step guide to loading a .php file via Ajax using JQuery when an item is selected from a dropdown

I have successfully populated a dropdown list using PHP from a database on the page named admin.php. Now, I am utilizing JQuery with Ajax functionality to display information when a surname is clicked from the dropdown menu. The goal is to call employerP ...

Troubleshooting problem with Angular4's HTTP requests

While working on my Angular 4 application, I am creating an oath guard service to check the validity of tokens. If the token is not valid, I want the user to log in again. Below are the functions I have implemented for this purpose: isLogedIn(){ ret ...

What steps can be taken to effectively build a test suite architecture using Jest?

After exploring all the available resources on the Jest documentation website, including various guides and examples, I have yet to find a solution to my specific question. I am in search of a methodology that would enable me to individually run test case ...

Understand which form has been submitted

I have eight Forms in a page (Form0, Form1, Form2 and so forth). Each form, when submitted, sends data to ReassignPreg.php using JavaScript, which then searches for the data in the database and returns it as JSON. The corresponding divs on the page are the ...

Navigate to a Vue router hash without refreshing the component and scroll to it seamlessly

I'm currently utilizing Vue 3 along with vue-router 4. Suppose I have hash links within a component: <router-link :to="{ hash: '#l1' }">Section - 1</router-link> <router-link :to="{ hash: '#l2' }" ...

Enable CORS for AJAX requests with RESTful web services in Google Chrome

My web-based project is fully written in jQuery and JavaScript. On the client side, I am calling RESTful webservices via AJAX like this: $.ajax({ type: 'GET', timeout: 1000000000, headers: { 'Access-Control-Allow-Origin': ...

Populate a table dynamically using JavaScript based on existing cell values

I am currently working on filling an HTML table based on the contents of its left cells. The code below creates a table that is populated with variables: <table id="myTable" ></table> <script> var rightcell = [{name : "Name ...

Understanding how events propagate in three.js

I am currently working on a scene that consists of multiple objects. To manipulate the selected object, I am using an orthographic camera controller. Specifically, I want to rotate the object with the mouse by pressing shiftKey + 1 (rotation around its own ...

Using Ruby instead of Javascript in lessc

My CSS is compiled in a node application using lessc. LESS was installed via NPM. When I check the current version of lessc --version it shows lessc 1.3.3 (LESS Compiler) [Ruby] 2.3.2 How can I change it to display lessc 1.3.0 (LESS Compiler) ...

Express server encountering difficulties locating requested file for download

I encountered a curious issue while working with my express server: I am attempting to download a file from a dynamically created folder. Below is the code snippet: var folder = id.toString() var file = [projectRoot,"/app/temp/",folder, "/regist ...

Implementing the 'not-allowed' cursor style on disabled rows in Material UI datagrid

I have a specific disabled row in a Material UI data grid where users are unable to select or perform any actions on it. I am looking to display the cursor as "not-allowed" on this particular row. How can we apply styling to only this row since there is no ...

A specialized identifier for nested objects in a React component

I am currently working with a JSON data structure that looks like this: [ [ { city: x patients: x id: 1 }, { city: y patients: y id: 2 } ], [ { city: x patients: x id: 1 }, { city: y patients: y id: 2 } ...

Following the submission of a POST request, an error occurred stating: "Unable to assign headers once they have been sent to

I'm having trouble figuring out what's wrong with my code. Whenever I send a post request, I get an error message saying "Cannot set headers after they are sent to the client". My model includes a comment schema with fields for user, content, and ...

The integration between React hook form and reactstrap input components is not functioning properly

Having an issue with react-hook-form and reactstrap. The component List.jsx is causing trouble: import { useContext, useEffect } from "react"; import { ListContext, ADD_LIST } from '../providers/ShoppingListProvider'; import { Link } from "react- ...