Using jQuery.dataTables to choose all rows except the final column

I'm utilizing a table with jQuery.dataTables. I have a requirement to input dates in the last column. However, when I click to input a date, the row becomes deselected.

The following code is meant for selecting all rows:

Displayed below is the DataTable setup:

var table = $('#tableRegister').DataTable({
                data: [],
                columnDefs: [{
                    orderable: false,
                    className: 'select-checkbox',
                    targets: 0
                }],
                select: {
                    style: 'os',
                    selector: 'td:first-child'
                },
                order: [[1, 'asc']],
                columns: [
                    { data: "ACTION" },
                    { data: "ID" },
                    { data: "REGISTER_DATE" }
                ],
                rowCallback: function (row, data) { },
                filter: false,
                info: false,
                ordering: false,
                processing: true,
                retrieve: true
            });
$('#table_name tbody').on('click', 'tr', function () {
                    $(this).toggleClass('selected');
                });

The values of the table are populated using ajax calls.

for (var i = 0; i < e.DataBadge._REG.length; i++) {
    table.row.add({
    "ID": e.DataBadge._REG[i]._ID,
    "REGISTER_DATE": `<input type='text' class='form-control datepicker2' id='register_date${i}'/>`
    }).draw();
}

Is there any way to maintain selection on the last column?

Answer №1

Whenever the date input is clicked, the row is no longer selected

It seems like clicking on the date picker also affects the row selection

Here is a code snippet demonstrating how clicking a button can change the row:

$("table tr").click(function() {
  $(this).toggleClass("selected");
});
tr.selected { background-color: yellow; }
td { padding: 6px; border: 1px solid #CCC; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr><td>A</td><td>B</td><td><input type='text'><button type='button'>pick</button></td></tr>
<tr><td>A</td><td>B</td><td><input type='text'><button type='button'>pick</button></td></tr>
<tr><td>A</td><td>B</td><td><input type='text'><button type='button'>pick</button></td></tr>
</tbody>
</table>

To prevent this behavior, you can use .stopPropagation(). Refer to https://api.jquery.com/event.stopPropagation/

$("table tr").click(function() {
  $(this).toggleClass("selected");
});

$("table button").click(function(e) {
  e.stopPropagation()
});
tr.selected { background-color: yellow; }
td { padding: 6px; border: 1px solid #CCC; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr><td>A</td><td>B</td><td><input type='text'><button type='button'>pick</button></td></tr>
<tr><td>A</td><td>B</td><td><input type='text'><button type='button'>pick</button></td></tr>
<tr><td>A</td><td>B</td><td><input type='text'><button type='button'>pick</button></td></tr>
</tbody>
</table>

Alternatively, instead of highlighting the entire row, you can choose to only highlight specific cells within the row and exclude the last column by modifying the click handler to target td elements instead of tr then toggle the class:

$(this).closest("tr").find("td").not(":last-child")

See example below:

$("table tr td").click(function() {
  $(".selected").removeClass("selected");
  $(this).closest("tr").find("td").not(":last-child").addClass("selected");
});
td.selected { background-color: yellow; }
td:not(:last-child) { cursor: pointer; }
td { padding: 6px; border: 1px solid #CCC; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr><td>A</td><td>B</td><td>C</td></tr>
<tr><td>A</td><td>B</td><td>C</td></tr>
<tr><td>A</td><td>B</td><td>C</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

Conceal the menu in Angular Material when the mouse leaves the button

When the mouse hovers over a button, a menu is displayed on the website's toolbar. The menu is set to close when the mouse leaves a surrounding span element. Now, there is an attempt to also close the menu when the mouse leaves the triggering button i ...

The background image fails to display on the button

Why isn't the background image for my button appearing when set using a CSS rule? I have a button with the class "todo" and although other parts of the rule work, the image itself does not show up even after trying "background: url" and "background-im ...

Adjusting SVG size based on the position of the cursor for transforming origin

My goal is to scale an SVG circle using the trackpad (by moving two fingers up and down), with the origin of the transform being the cursor position. Initially, the scaling works as intended, but on subsequent attempts, the circle changes position, which s ...

Tips on Implementing a CSS Variable in a React Component

Is there a way to use content variable with in-line styles in React? I am unsure of how to accomplish this. CSS3 .right::after, button::after { content: var(--content); display: block; position: absolute; white-space: nowrap; padding: 40px 40p ...

The issue with WooCommerce's update_checkout function is that it is not properly updating the available shipping methods

My WooCommerce site includes a function that adds a 4€ fee when the cash on delivery payment method is selected: add_action('woocommerce_cart_calculate_fees', 'increase_cod_cost'); function increase_cod_cost() { if(WC()->sessio ...

There is no data in the $_POST array

Currently, I am developing a simple application running locally with XAMPP and PHPStorm v9. The issue I am facing is that the server handler for my AJAX request is returning an empty $_POST array. This problem has left me puzzled, and I cannot figure out w ...

What is the method to process a JSON path containing a special character like "@"?

I'm attempting to retrieve JSON data from an API with the following structure. My current approach involves using JavaScript. The code snippet I have utilized is displayed below. <p>"stations": [</p> <p id="data"></p> ...

What are the steps to gather user data and generate a roster of individuals currently online?

I am currently working on a way to gather information about the users who are currently logged into my website. Here's how it works: When a user enters my website, they have the option to choose a nickname using an input box. Then, there are five diff ...

Boosting your website with a slick Bootstrap 4 responsive menu that easily accommodates additional

I have incorporated a main menu in my website using the Bootstrap 4 navbar. However, I also have an additional div (.social-navbar) containing some extra menu items that I want to RELOCATE and INSERT into the Bootstrap menu only on mobile devices. Essentia ...

Unforeseen execution issues arising from repeated Ajax calls with SetTimeout in JavaScript

I have a list of users displayed in an HTML table that is dynamically created on page load. Each row includes an inline button with onclick="functionName(userId)" that triggers the following actions: When clicked, a Bootstrap modal popup is shown and the ...

Creating a responsive layout that sets the window height to 100% and automatically adjusts sibling divs when the content within parent divs exceeds the defined height

<section> <div class="section-wrap"> <h1>page1</h1> </div> </section> <section> <div class="section-wrap"> <h1>page2</h1> </div> </section> My attempt at impleme ...

What is the best way to get both the id and name of a selected item in Angular?

Within my select field, data is dynamically populated based on names. My objective is to not only capture the selected name but also its corresponding ID. Here's a snippet of my HTML template: <select class="custom-select d-block w-100" id="test" ...

Unleash the power of attribute selectors in a SASS/SCSS list variable: a comprehensive guide!

This is a sample code snippet: $list: ( input[type="text"], input[type="name"], input[type="email"], textarea, select[multiple] ) !default; @each $value in $list { ...

What is the best way to define a variable that captures and logs the values from multiple input variables?

Hey there, I'm a new coder working on a shopping list app. I'm trying to display the input from three fields - "item", "store", and "date" - at the bottom of the page as a single line item. I attempted to do this by creating a variable called "t ...

The FaceBook modal is failing to appear

Attempting to implement FaceBook Login through a modal. Upon clicking the button below, the FaceBook Login Modal should appear. <li> <a href="#sign-in-dialog" class="login sign-btn" title="Sign In">Sign In</a> </li> The HTML co ...

IE8 experiencing issues with jQuery live click event functionality

I have this code snippet that is working perfectly fine in all browsers except for IE8. The #cal_popup_table element is dynamically added to the page. $("#cal_popup_table tbody tr td a").live('click', function() { $('.da ...

Sending a null variable via an AJAX post request from jQuery to PHP

Query: <table> <tr> <td id="percentage">Percent:&nbsp; <?php echo $percent; ?> %</td> </tr> </table> <div class="box-footer" style="float: right"> <a href="<?php echo base_url(); ?>stu ...

What could be causing the text-end to fail in Bootstrap 5?

Why can't I align the text to the right? Feeling frustrated as a beginner. <div class="top-bar"> <div class="container"> <div class="col-12 text-end"> <p><a href="tel:06 ...

What is the best way to create a scrollable Material UI modal and dialog?

Having a problem with my mui modal where the top content is getting cut off and I can't scroll up. I've tried adding the {overflow:"scroll"} property to the modal but it's not working. Here's the code snippet I'm currentl ...

Execute the function when the element comes into view

Is there a way to create a function that will automatically attach itself to an element when it comes into view? I have multiple elements on my page - element1, element2, and element3. Instead of using a large if statement, I would like to write a functio ...