Is there a way to prevent bootstrap collapse from being triggered when clicking on a link inside the collapse element?

Utilizing the Twitter Bootstrap collapse feature to conceal a table row. The issue arises when there is a link within the text that triggers the collapse/expand behavior. Upon clicking the link, the collapsed text first expands and then the link is followed.

To resolve this problem, I am looking for a way to prevent the collapse toggle from being triggered when clicking on the link. Below is an example code snippet showcasing the problem (view live jsfiddle example here):

<table class="table">
    <tr data-toggle="collapse" data-target="#collapseRow1" style="cursor: pointer;">
        <td>Click me to show more!</td>
        <td><a href="http://getbootstrap.com/css/">but don't expand after clicking this link</a></td>
    </tr>
    <tr class="collapse" id="collapseRow1">
        <td>hidden content1</td>
        <td />
    </tr>
</table>

Answer №1

Prevent event propagation by using event.stopPropagation() when handling click events.

Learn more about event.stopPropagation()

$("table.table td a").on('click', function (e) { e.stopPropagation(); })

To gain insight into why events bubble up the DOM tree, refer to this resource

Answer №2

If you want to designate a link as an outside link, you can simply add a class to the anchor tag like this:

<a class="outsidelink" href="http://getbootstrap.com/css/">
. Then in your JavaScript code, you can handle it like so:

$(".outsidelink").on('click', function (e) { e.preventDefault(); })

UPDATE: An important note from @MustDie1Bit - using preventDefault() will block all events. You may use e.stopPropagation(); instead.

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

Animated the height of a rectangle during initial loading and when the mouse hovers over or leaves

Being new to Vue.js, I am looking to create a simple animation on component load for the first time. You can find my initial code here: <template> <div id="app"> <div class="rect" /> </div> </template ...

top technique for displaying videos and images in a lightbox

In the process of developing a web page, I am utilizing both JQuery and Prototype. My goal is to incorporate Lightbox for playing flash movies but I have run into an issue because I am already using Lightbox to showcase images. Is there a way to use Ligh ...

Why does the Node.js REST API keep throwing errors after I try to create just one record?

I encountered a back end issue recently. I have developed a simple API with just a post endpoint. The intention is to post data using the req.body. Oddly enough, it works perfectly fine the first time but when I attempt it again, I receive an error message ...

How to dynamically load content with jQuery when the back button is triggered

Utilizing jQuery to dynamically load page content when a link is clicked has been successful. Here is the code snippet: $(document).on('click', 'nav a', function (e) { e.preventDefault(); var url = $(this).attr("href"); wi ...

Tips on incorporating personalized javascript functions into Durandal

I am currently working on my first project using the Durandal framework to create a basic website. I have encountered an issue while trying to incorporate a simple JavaScript function. My goal is to execute the function after the DOM has loaded, but I am u ...

Discovering the X and Y coordinates on a Half Doughnut Chart using Chart JS in React

I have successfully created the doughnut section of the chart along with the gauge needle. Now, I am looking to enhance it by adding a circular pointer instead of the needle. Although I was able to draw the circular pointer, I am facing difficulty in deter ...

What is the method to spin an item in three js while keeping its axis in focus?

Looking to rotate a globe object around its y-axis smoothly? I have come across a helpful function for achieving this: function rotateAroundObjectAxis(object, axis, radians) { var rotationMatrix = new THREE.Matrix4(); rotationMatrix.makeRotationAxis ...

The chosen Material UI tab stands out with its distinct color compared to the other tabs

Just dipping my toes into the world of Material UI, so bear with me if this question sounds a bit amateur. I've been playing around with Material UI Tabs and successfully tweaked the CSS to fit my needs. Take a look below: https://i.stack.imgur.com/B ...

Achieving sliders similar to those in the "level" window of GIMP can be done by customizing the shape of GtkScale in your C program

I have a query regarding the shape of GtkScale. Specifically, I am interested in making my GtkScale resemble the one found in the levels window of Gimp. To clarify: a bar 3 triangles (representing shadows, midtones, and highlights) https://i.stack.imgur ...

Setting a background-image using jQuery in Codeigniter can be done by following these steps

Currently, I am developing a project in Codeigniter. In my jQuery file, I have included code to set the background image of the body element: $('body').css('background-image','url(<?php echo base_url("assets/images/bg2.png");?& ...

JavaScript function to identify and highlight duplicate values within rows

In my current project, I am utilizing JavaScript to identify and highlight duplicate values within a table. The functionality of the code involves storing table row values in an array, checking for any duplicates, and then highlighting those rows accordin ...

Showcasing a dynamically created JSON document on a basic web page

Looking for a solution to display the contents of a result.json file generated by my Java tool on a simple website. I am aware that accessing local files directly with JavaScript is not possible, so seeking alternative methods that do not involve using a w ...

Incorporating images with JavaScript in Jade templates

Currently I have a layout.jade code that I am looking to modify the javascript for. My goal is to load an image onto the page every time it is reloaded using jade. Here is the existing code: html head title= title script(src='/libs/jquery/j ...

The Android WebView experiencing issues with the functionality of the Hamburger Menu

I'm facing an unusual issue with the Android WebView in my app. The hamburger menu on my website works perfectly fine on my mobile browser, but when I try to open it in the Android WebView, it does not fully expand to show the menu items. I have trie ...

When attempting to PUT a JSON object to a specific URL, an error occurs

One of my current tasks involves creating a function to send requests to a specific URL function json_object_todo(url, method, param_object, dataObj) { var json_obj; $.support.cors = true; try { var ajax_params = { type: me ...

Tips for choosing and controlling Google Maps markers using their unique identifiers (V3 JS API)

Currently, I have a Google Map that displays markers on the left side and a list with explanatory content items corresponding to each marker on the right side. When a marker is clicked, the list will automatically scroll to the relevant entry. This functio ...

What is the preferred method for $_POST to handle multiple parameters when they are passed as a single string?

Trying to implement an Ajax call using JQuery, PHP, and MySQL. The task involves sending two variables: $speaker_id and $article_id. The jQuery construction looks like this: $('.ajax-call').click(function() { var speakerID = <?=$speaker ...

Tips for sending refs to components that load lazily

Hey everyone, I'm currently working on passing refs to a lazy load component but running into an issue where I'm receiving null instead of {current: null}. Can someone help me identify where I might be making a mistake? 'use client'; im ...

Using the useState() hook with a component as an argument

I have a unique idea for my app - I want the App component's state to store another component that it will render. This way, any component should be able to update the state and trigger a re-render of the App component. function HomePage() { retur ...

Progress bar designed for simultaneous ng-apps on a single webpage in Angular

I have been utilizing the angular loading bar plugin, but I have encountered a challenge when trying to use it on pages with multiple ng-app declarations. https://github.com/chieffancypants/angular-loading-bar Currently, I am only able to add the depende ...