When the button within a clickable row is pressed, the row will be highlighted

I have implemented a Bootstrap table where each row is clickable. Within one of these rows, I have added a button that serves as a link to another location. However, when I click on the button, the entire row becomes highlighted.

After some research, I discovered that in order to prevent this row highlighting behavior, I may need to implement jQuery for pagination control. Below is the current jQuery code I am using to toggle the clickable row:

$(document).ready(function() {
            $('.myTable').on('click', '.clickable-row', function(event) {
                $(this).toggleClass('table-warning');
            });
        });

I am unsure about what modifications need to be made to ensure that the row does not get highlighted when clicking on the button.

EDIT:

<table class="table table-hover" id="myTable">
  <thead>
    <tr class="clickable-row">
      <th scope="col">#</th>
      <th scope="col">First</th>
      <th scope="col">Last</th>
      <th scope="col">Handle</th>
    </tr>
  </thead>
  <tbody>
    <tr class="clickable-row">
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr class="clickable-row">
      <th scope="row">2</th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr class="clickable-row">
      <th scope="row">3</th>
      <td colspan="2">Larry the Bird</td>
      <td><a class="btn btn-primary" href="#" target="_blank" role="button">Link</a></td>
    </tr>
  </tbody>

Answer №1

If you've come across the advice to stop propagation, not pagination, it simply means preventing the current event from further propagating in both the capturing and bubbling phases. In the context of your example, this translates to ensuring that a click on a button within a table does not also register as a click on the entire row. You can learn more about this concept by visiting stopPropagation(). Additionally, note that I've included the use of event.preventDefault(); in the sample code to avoid triggering a broken link scenario. When implementing this in your actual code with a valid link address, make sure to remove this line.

$(document).ready(function() {
  $('#myTable').on('click', '.clickable-row', function(event) {
    $(this).toggleClass('table-warning');
  });
  
  $('#myTable').on('click', '.btn', function(event) {
  event.preventDefault();
  event.stopPropagation();
  $(this).parent("td").toggleClass("clicked");
  });
});
.table-warning {
  background-color: yellow;
}
.clicked {
  background-color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-hover" id="myTable">
  <thead>
    <tr class="clickable-row">
      <th scope="col">#</th>
      <th scope="col">First</th>
      <th scope="col">Last</th>
      <th scope="col">Handle</th>
    </tr>
  </thead>
  <tbody>
    <tr class="clickable-row">
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr class="clickable-row">
      <th scope="row">2</th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr class="clickable-row">
      <th scope="row">3</th>
      <td colspan="2">Larry the Bird</td>
      <td><a class="btn btn-primary" href="#" target="_blank" role="button">Link</a></td>
    </tr>
  </tbody>
  </table>

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

Encountering an error when utilizing the ForEach method

Encountering a problem with using forEach: I am receiving an error message stating "unhandledRejection: TypeError: data.forEach is not a function" Here is the solution I attempted: I converted the data into JSON format before using forEach const data = JS ...

Updating an if statement in Rails views via AJAX: the ultimate guide

In my app, I have votable posts that users can vote on through AJAX. While this functionality works well, I also want to update the status of a post (indicating whether it is an agreed milestone) when a user votes through AJAX. Below is the code snippet fo ...

Detecting Pixel Colors Across Multiple Overlapping Canvases

I have a challenge with multiple canvas elements on my webpage. I am trying to retrieve the pixel color of all overlapping canvas elements when they are stacked on top of each other. Let me illustrate with an example below. In this scenario, I am attempt ...

Pass along computed style data to the parent component in Vue.js

I am relatively new to VueJS and currently in the process of exploring its features. One specific issue I am facing on my website involves a TopBar component that includes both the logo and the menu. <template> <div id="topBar"> <div ...

Having trouble adding elements (after the initial iteration) using the push() method in JavaScript

I'm facing an issue where I have a string and an array, but I'm unable to push elements after the first iteration. Below is the code I'm using: response1 = "hello"; var arr = [""]; arr.push(response1); console.log("First position " + arr[0 ...

Transforming a JSON object into an "array" format

I have encountered a formatting issue when trying to structure data for plot.ly as follows: var data = [ { x: [cow, horse, chicken] y: [20, 14, 23], type: 'bar' } My current approach to retrieve data from mySQL is shown below: ...

The mega menu is malfunctioning differently across various browsers and causing issues with responsiveness

I've encountered a strange issue with my portfolio website. While everything works smoothly in Google Chrome, I'm experiencing problems with the mega menu responsiveness on other browsers like Firefox, Microsoft Edge, and Opera Mini. Visit the ...

How to send information with jquery through php and save it in mysql database

For some reason, I am encountering an issue with writing to the database when creating new registrations. Below is a snippet of my JavaScript code that includes both login and registration components: <!DOCTYPE html> <html> <head> <t ...

Exploring ways to utilize Next.js (React) for formatting date and time with either Moment.js or alternate

When deciding on the best method for handling date formats in my next front-end app, should I use Moment.js or JavaScript functions? The data is sourced from the backend as the date type, and I want it to be displayed in a user-friendly format while consid ...

Having problems with loading @font-face in Ionic Framework Android app build?

While working on an app in Ionic Framework, I encountered a peculiar issue. https://i.stack.imgur.com/xk8uD.png In my Ionic Framework application, I am importing @font-face and running the app on an Android device (Galaxy SIII). Strangely, the font (Mont ...

Typescript implementation for a website featuring a single overarching file alongside separate files for each individual webpage

Although I've never ventured into the realm of Typescript before, I am intrigued by its concept of "stricter JS". My knowledge on the subject is currently very limited as I am just starting to experiment with it. Essentially, I have developed my own ...

Attempting to insert a line break tag within the text using React

Having trouble inserting line breaks in text using React. Can anyone guide me on how to achieve this? I've attempted adding the br tag, but it is displaying as in the browser. Here's a snippet of my code. Appreciate any help or advice. let sp ...

What are the steps to achieve consistent response behavior in POSTMAN that matches that of a web browser?

Below is an example of my code: const express = require('express'); const app = express(); app.get('/', function (req, res) { res.setHeader('Content-Type', 'text/html'); res.write("First \n"); set ...

Enclose specific content items with double div elements using JavaScript every nth time

I have been attempting to encapsulate two divs around every set of three content pieces received from an API call. The desired structure is as follows: <div class="carousel-inner"> <div class="item carousel-item"> <div cla ...

Ensuring Smooth Transfer: Sending Local Storage Data to MVC Controller

I'm attempting to send an array of strings from my local storage (key value) to an MVC controller. Here's the code I have so far: Inside the cshtml View file: <script> function getFavouriteBooks() { var ids = JSON.par ...

Enhancing the functionality of ng-click within ng-repeat

I am seeking a solution to enhance the functionality of the ngClickDirective by implementing a custom listener. The provided code successfully works with ng-click elements that are not nested within ng-repeat: $provide.decorator('ngClickDirective&apo ...

Creating a layered effect: Adding an image onto another image using HTML and CSS

Wondering if it's possible to create an overlaid effect with an image and text inside of an <img> element, similar to the example image below. Utilizing Bootstrap for this design. This is how I am attempting to achieve it: HTML <div class= ...

Firebase allows for the updating of an object within a nested array

Within Firestore, I have a Document that includes a property named "items" which is of type array. This array consists of ShoppingItem objects with the specified structure: export class ShoppingItem { id?: string; name: string; checked = false; } To ...

What is the best way to send a populated custom class from JavaScript to a .NET MVC app using AJAX?

I am working with a .NET class in C# called BusinessModel: public class BusinessModel { public string BlobName { get; set; } public string NewName { get; set; } } In my MVC Ajax Controller, I have an action called DoBusiness: [HttpPost] public A ...

How can we develop play buttons for Facebook timelines similar to those found on Spotify?

Is it possible to create an HTML5 player that can be embedded on Facebook timelines, similar to the way Spotify has special privileges as a Facebook partner? I'm looking to provide a list of play buttons that, when clicked, will play songs sequentiall ...