Trouble activating Bootstrap 4 checkbox through JavaScript integration

When clicking on a table row, I am able to add the class active with JavaScript. However, when trying to click on a checkbox, an error occurs.

Check out the live Codepen link here

Below is the custom code snippet:

$('.dashboard-table-tbody tr').click(function() {
if($(this).hasClass('active')){
$(this).removeClass('active');
$(this).children('td').children('div').children('input').prop('checked', false);
}
else{
$(this).addClass('active');
$(this).children('td').children('div').children('input').prop('checked', true);
}
});
.dashboard-table{
margin-bottom: 0;
font-size:13px;
line-height:15px;
color:#8b8e8c;
}
.dashboard-table th{
border:none;
padding:8px 12px;
font-weight:600;
}
.dashboard-table td{
padding:12px 12px 12px 12px;
vertical-align: middle;
}
.dashboard-table a{
font-weight:600;
}
.table-checkbox-col-head{
width:44px;
}
.table-checkbox{
padding: 0;
min-height: auto;
width: 16px;
height: 16px;
margin: 0 auto;
display: inline-block;
vertical-align: middle;
}
.table-checkbox .table-checkbox-label{
width: 16px;
height: 16px;
}
.table-checkbox .table-checkbox-label:before, .table-checkbox .table-checkbox-label:after{
top: 0;
left: 0;
margin-top: 0;
}
.dashboard-table tbody tr:hover {
background-color: #fafbfe;
}
.dashboard-table tbody tr.active {
background-color: #f8f9fc;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-sm dashboard-table">
<thead>
<tr>
<th scope="col" class="table-checkbox-col-head">
<div class="custom-control custom-checkbox defaultCheckbox table-checkbox">
<input class="custom-control-input" id="customControlInline2" type="checkbox">
<label class="custom-control-label table-checkbox-label" for="customControlInline2"></label>
</div>
</th>
<th scope="col" class="p-l-0">Name</th>
<th scope="col">Size</th>
<th scope="col">Upload at</th>
<th scope="col" class="text-right">Views</th>
</tr>
</thead>
<tbody class="dashboard-table-tbody">
<tr>
...
[/omitted for brevity]
...
</tr>
</tbody>
</table>

Answer №1

In order to prevent the tr click event from triggering twice, be sure to add e.preventDefault();.

Here is a detailed demonstration of the necessary changes:

$('.dashboard-table-tbody tr').click(function(e) {

    if ($(this).hasClass('active')) {
        $(this).removeClass('active');
        $(this).children('td').children('div').children('input').prop('checked', false);
    } else {
        $(this).addClass('active');
        $(this).children('td').children('div').children('input').prop('checked', true);
    }

    if ($('.table tbody').find('input:checkbox:checked').length === $('.table tbody').find('input:checkbox').length) {
        $(".headerCheckbox").prop('checked', true);
    } else {
        $(".headerCheckbox").prop('checked', false);
    }
    e.preventDefault();

});

$(".headerCheckbox").click(function(e) {

    if ($(this).is(":checked")) {
        $(".table tbody tr").addClass("active");
        $(".table tbody tr input:checkbox").prop('checked', true);
    } else {
        $(".table tbody tr").removeClass("active");
        $(".table tbody tr input:checkbox").prop('checked', false);
    }
});
.dashboard-table{
margin-bottom: 0;
font-size:13px;
line-height:15px;
color:#8b8e8c;
}
.dashboard-table th{
border:none;
padding:8px 12px;
font-weight:600;
}
.dashboard-table td{
padding:12px 12px 12px 12px;
vertical-align: middle;
}

... (CSS styles continue)

 

You can find the HTML and script that accompanies these changes above in the provided code snippet.

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

Error Encountered in Vue.js when Trying to Access a Nested

Below is a snippet of my route code from app.js let routes = [{ path: "/dashboard", component: require("./components/Dashboard.vue") }, { path: "/tour", component: require("./components/Index.vue"), children: [{ name: &apos ...

Show a dynamic highchart graph displaying linear data retrieved from the database

I am attempting to present data retrieved from a database in a linear highchart format. Here is the JSON response from my database: [{"protocol":"tcp","date":"01/02/20","time":"00:10:20","total":281}, {"protocol":"udp","date":"01/02/20","time":"00:10:30", ...

Guide on triggering a Bootstrap4 popup to appear upon window load in the absence of a set PHP session variable

Is there a way to trigger the opening of a bootstrap4 popup on window load only if the php session variable is not set? session_start(); if(!isset($_SESSION['templateid'])){ // code to open popup } Any suggestions on how to achieve this u ...

Is there a workaround available for the JavaScript timeout and interval functions in XPages SSJS?

While utilizing XPages' server-side JavaScript (SSJS), I find myself longing for the timing/scheduling features like setTimeout, setInterval, clearTimeout, and clearInterval. Has anyone developed a polyfill to add these crucial functions to SSJS? ...

"Utilizing AJAX to establish a connection between database and web application using PHP and

I am new to using ajax and I'm trying to understand this example, which is similar to what I want to achieve. I have some questions about the sample code: Is it necessary to include a specific ajax header script to use it? What does this part do ...

The combination of Thymeleaf, Spring Boot, and Bootstrap may encounter issues when used in a page containing the PathVariable

I am currently running an application using Spring Boot 2.1.9.RELEASE, Thymeleaf, and Bootstrap 4. 1) I have a base.html page that serves as a template: <!DOCTYPE html> <html lang="pt-br" xmlns:th="http://www.w3.org/1999/xhtml" xmlns ...

Switching Between Olson/tz Timezone and Universal Time in Node.js

Imagine I need to work with a specific Olson/tz database timezone, let's say on July 1st, 1973 at 15:23 in Africa/Maputo. Could anyone guide me on how to convert this time to universal time UT and back again using node.js? While I've come across ...

What is causing this error to appear in Next.js? The <link rel=preload> is showing an invalid value for `imagesrcset`

I've got a carousel displaying images: <Image src={`http://ticket-t01.s3.eu-central-1.amazonaws.com/${props[organizationId].events[programId].imgId}_0.cover.jpg`} className={styles.carouselImage} layout="responsive" width={865} ...

Oops! Make sure to explicitly allow the dependency @types/html2canvas by adding it to the "allowedNonPeerDependencies" option

After installing the html2canvas package in my Angular library project, I encountered an error when compiling in production mode using the command ng build --prod. The specific error message is as follows: ERROR: Dependency @types/html2canvas must be exp ...

When the <div> element appears, it should cause the images below to be pushed downward

I have an HTML file where clicking on an image opens a box with text below it using jQuery. Is there a way to push the images under the box down to ensure that the box never covers an image? It needs to be responsive as well. Here is the fiddle link: Bel ...

Differentiating Between Observables and Callbacks

Although I have experience in Javascript, my knowledge of Angular 2 and Observables is limited. While researching Observables, I noticed similarities to callbacks but couldn't find any direct comparisons between the two. Google provided insights into ...

What is the sequence of the middlewares for error handling and handling of 404 errors?

The express.js website has confused me with contradictory statements regarding error-handling middleware. According to one page, you should define error-handling middleware last, after other app.use() and routes calls. However, another page states that you ...

The image is not appearing in my small AngularJS application

I have developed a small application that converts Fahrenheit to Celsius and I am trying to display a relevant image based on the Fahrenheit temperature input. However, I am facing difficulties in getting the image to display correctly. Can someone please ...

When using ngClick with a parameter, the parameter is not being successfully passed

My table resembles a tree structure with two ng-repeats. <table> <tr ng-repeat-start="am in anArray"> <td><button ng-click="TheFunction(am)"></button></td> </tr> <tr ng-repeat-start="em in anotherArray"> < ...

"Creating a Two-Column Layout with ASP.NET Core and Bootstrap through POST

I have been working on a simple form for testing and learning purposes. Initially, I used Scaffold New Razor Page in VS to create the form. Afterward, I attempted to modify it in order to have two columns on the create page. Despite trying various methods ...

Error encountered while running a Javascript application in Selenium IDE

Occasionally, I encounter this issue while executing test cases in Selenium IDE: An unexpected error occurred. Message: TypeError: testCase.debugContext.currentCommand(...) is undefined Url: chrome://selenium-ide/content/selenium-runner.js, line ...

What is the best way to create a fluid-like moving 2D morphing circle using three.js?

I have a circular mesh that I want to animate similar to the example shown in this link from the two.js 2D library: Currently, I've been studying this example and positioning the camera above the shape, but I believe there must be a simpler way to a ...

Overlay Image on Card, Overflowing into Card Body

I currently have a webpage featuring cards. Each card includes a thumbnail at the top with an overlay. The main content of the card needs to be positioned outside of the overlay, but it ends up overflowing onto it. Take a look at the demo: https://codepe ...

Using a variable value as a regular expression pattern: A beginner's guide

I'm at my wit's end - I can't figure out where I'm going wrong. I've been attempting to replace all instances of '8969' but I keep getting the original string (regardless of whether tmp is a string or an integer). Perhaps ...

"Troubleshooting the absence of transitions within the sidebar component in bootstrap-vue

I have integrated the b-sidebar component from the bootstrap-vue library into my Vue 2 application: "dependencies": { "vue": "^2.7.7", "bootstrap": "^4.6.1", "bootstrap-vue": "^2 ...