"Interactive functionality: Toggling checkboxes with a DIV click

I am trying to set up a simple div container that displays contact information in a formatted list (<ul><li>). Clicking on the div currently takes you to the user's profile page, which is functioning correctly.

However, I am facing an issue where clicking on the checkbox inside the div also triggers the redirection to the profile page. Is there a way to make the checkbox still clickable without redirecting the user away from the current page?

You can see a mock-up of my design on CodePen: CodePen

HTML

<div class="contactlist" data-token="ABCDEF">
    <ul>
        <li><input type="checkbox"></li>
        <li><img src="" alt="..." height="50" width="50" /></li>
        <li><span>Mr A Sample</span></li>
    </ul>
</div>

JS

$(".contactlist").on("click", function(e){

  var token = $(this).data('token');
  window.location.href = "contact.php?token="+token;

});

Just a note, the example on CodePen does not actually redirect (they have disabled it), but the code should normally work as described.

Answer №1

The event object contains a target property that points to the element from where the event originated (in this case, the clicked element). If the originating element is a checkbox, you can bypass the rest of the event logic by checking for it.

// skipping if not checkbox
if(!$(e.target).is(':checkbox'))

Complete JavaScript code

$('.contactlist').on('click', function(e) {  
    if(!$(e.target).is(':checkbox')) {
      var token = $(this).data('token');
      window.location.href = 'contact.php?token=' + token;
    }
});

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

The sticky navigation bar allows for the overlapping of additional div elements

Here is the HTML AND SCSS(SASS) code for a NAVBAR: <div id="navbar"> <ul> <li><a href="#home">Home</a></li> <li><a href="#main">Main</a></li> ...

What is the best way to transmit a JSON object from a Python code to jQuery?

After searching through various APIs and resources, I'm still struggling to properly fetch a JSON object from a Python script using AJAX. It seems like the issue lies in how I am handling the JSON object. To begin, I have a python script on my server ...

Passing arguments to an external function in jQuery from a dynamically loaded Ajax page

Despite its confusing title, the issue at hand is actually quite simple. My homepage contains a script that loads an external PHP file for a specific section of my website. Within this PHP file, I need to call a function from the main JavaScript file (th ...

Injecting a component in Angular 2 using an HTML selector

When I tried to access a component created using a selector within some HTML, I misunderstood the hierarchical provider creation process. I thought providers would look for an existing instance and provide that when injected into another component. In my ...

The issue of try-catch not functioning properly within a recursive function

Goal My objective is to capture the "Failed to load resource" error and display it in an alert window. Problem I am attempting to handle a video that does not exist. The try catch block is not catching the exception. Scenario I am working on playing vide ...

Issue encountered: Compilation error occurred following the update from Angular 11 to Angular 15, stemming from the module build failure within the sass-loader directory

I recently attempted to update angular from version 11 to 15, but encountered an error as shown in the screenshot below ./src/assets/styles.scss - Error: Module build failed (from ./node_modules/sass-loader/dist/cjs.js): SassError: expected "(". ...

Identify whether the final digit falls within the range of 1-4 or 5-9, and then apply styling to the corresponding

First, this is the structure of my table: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <table class="table group-9569" width="100%"> <tbody> <tr> <td class=" ...

Tips for distinguishing the beginning and ending points of wrapped text in the Firefox browser

Within my work, I am utilizing a contentEditable span where I aim to position an element with position: absolute on the same line as the cursor. However, issues arise when text wrapping occurs - causing abnormal behavior at the start and end of wrapped lin ...

Creating personalized checkboxes

Trying to create custom checkboxes for different colors like red, blue, etc... https://i.sstatic.net/IKqJH.jpg Struggling to achieve the desired outcome Utilizing Bootstrap 5.3: Attempting to learn by dissecting a bootstrap template with the desired res ...

Adding an additional stroke to a Material-UI Circular Progress component involves customizing the styling properties

I am attempting to create a circular progress bar with a determinate value using Material-UI, similar to the example shown in this circular progress image. However, the following code does not display the additional circle as the background: <CircularP ...

Bringing data from HTML into Matlab via a table

I need assistance with importing data from an HTML webpage into Matlab. I've tried using the "getTableFromWeb" function, but it doesn't seem to be working as expected. When I view the data in a browser like Firefox, everything appears fine. Howev ...

Creating a task management system using HTML, CSS, and JavaScript that utilizes local

I have been extensively researching how to create a to-do list using JavaScript with local storage, but unfortunately, I have not been able to find exactly what I am looking for. Is there a way for me to set up a to-do list and input data before actually ...

Modify the appearance of the button

Currently, I have a setup where I have three buttons and three panels. When I click on Button 1, Panel 1 loads while Panel 2 and Panel 3 become invisible. Similarly, when I click on Button 2, Panel 2 loads while Panel 1 and Panel 3 are hidden. I would li ...

Click on the entire table to be redirected to the specified link

Currently, I am facing an issue with styling my tables as links. I have 3 tables and I want each table to be a clickable link. However, after implementing the links, I am unable round the corners of the tables. I envision the final look to be similar to t ...

Spacing Problem with Title Tooltips

After using the padEnd method to ensure equal spacing for the string and binding in the title, I noticed that the console displayed the string perfectly aligned with spaces, but the binded title appeared different. Is it possible for the title to support s ...

Tips for handling two JSON array objects with JavaScript

This particular function public function groups_priviledges($user_id = NULL){ $data['groups'] = $this->priviledges_model->admin_groups(); $data['member'] = $this->priviledges_model->empAdminGroup($user_id); echo ...

Running two blocks of scripts (utilizing lab.js as a loading manager)

I am currently facing an issue where I am trying to load two separate blocks of `lab.js` in different locations. However, when I attempt to utilize functions from the second block that are called from files loaded in the first block, they are showing as un ...

Tips for merging two classes in CSS

Within my code, I have two classes named classA and classB. I am looking to have classB inherit the style of classA while still only using classB. .classA{ width:100px;} .classB{ height:100px;} <div class="classB"></div> ...

Using Flask to invoke a Python function that displays an image when a button is clicked

I am currently working on developing a basic web application that allows clients to upload images, processes them on the server, and then returns the processed image to be displayed in a specific div. I attempted to use ajax for this purpose, but encounter ...

Storing JSON responses from a web service into a database using Linux wget in the background

I encountered a major issue. I have written a script in jQuery to communicate with a webservice, sending JSON data and receiving JSON responses in return. Here's a snippet of the code: function Product(date_from,date_to,API_KEY) { var self = this; se ...