Omit the element from the event listener

Is there a way to prevent the image from triggering a click event in this code snippet?

$('#templtable .trhover *:not(#infoimg)').live('click', function(event) {
      event.stopPropagation();
      $(this).toggleClass('selected');

$('#infoimg').click(function(event) {
          console.log("infoimg");
    event.stopPropagation();
});

Below is the structure of the table:

<table><tr class='trhover'><td><img id='infoimg' src='/images/icon-info.gif' alt='Details'><input type='checkbox' class='followup' id='1' value='234234'></td><td>234234</td></tr><table>

Despite attempting to exclude the image, the image still retains its click event functionality. Any ideas on how to fix this issue?

Appreciate any help you can provide.

Answer №1

It appears that your code is not valid. However, if you wish to prevent the img element from triggering the click event, you can use the following selector: $('tr *:not(img)'). Please note that this will still trigger the event when clicking the img due to event bubbling. To prevent this, you must bind an event to stop the propagation.

$('.trhover *:not(#infoimg)').bind('click', function(event) {
    event.stopPropagation();
    $('body').append('click -> '+event.currentTarget.tagName+'<br />');
});
$('#infoimg').click(function(event) {
    event.stopPropagation();
});

You can test this solution here: http://jsfiddle.net/pNaaD/
For a version with added rows, try this link: http://jsfiddle.net/Zxk8R/

Answer №2

Oh no, your code needs some fixing...
First of all, make sure to close your <tr> tag properly and remove that extra </td>.
Secondly, using end() cancels your filter unnecessarily and remember that live() is deprecated now - switch to using on() instead.
If you want the click event to work on elements with class .trhover, try this:

$('.trhover').on('click', function(){});

If you don't want <img> elements to respond to click(), follow czerasz's suggestion.
Also, why are you filtering the checkbox? What should it be doing?

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

Response received from PHP POST request using Ajax

Here is the ajax code that I am currently using: $.ajax({ url: apiURL, type: 'POST', data: {data:cart}, success: function(response){ console.log(response); }, error: function(error){ throw new Error(' ...

Tips for positioning a Wordpress navigation bar to the right of a logo

My goal is to position the navigation bar next to the logo in my Wordpress theme using Underscores. I have successfully aligned the primary navigation and the logo as desired with the following CSS: .main-navigation { position: relative; float: ri ...

CSS - Combining underline, striikethrough, and overline effects with customized styles and colors within a single element

I am trying to achieve a unique design with only one <span> that has three different text decorations (underline, strikethrough, and overline) like this: (This is just an example, I need it to be changeable) https://i.stack.imgur.com/61ZnQ.png Ho ...

Enhance the functionality of jQuery within a React application by creating custom chained jQuery functions

Before criticizing me, please hear me out: I understand that React can replace a majority of the functionalities provided by jQuery. However, I need specific interactions to occur when users hover over items, and as far as I know, React does not support th ...

Compiling 'react-scripts' to include a few images as data URIs within the CSS file

I have recently inherited a sizable React project, even though my experience with React is limited. Nonetheless, I am attempting to make improvements in areas where I feel confident. An ongoing issue we are facing is the excessive size of our main CSS fil ...

Guide to leveraging clsx within nested components in React

I am currently using clsx within a React application and encountering an issue with how to utilize it when dealing with mappings and nested components. For instance: return ( <div> <button onClick={doSomething}>{isOpened ? <Component ...

Issue with IE7 causing div elements to slide off the screen on specific WordPress pages

Encountering a strange IE7 bug on two pages of a WordPress site I'm currently developing. Some divs are shifting to odd positions, with one completely off the screen: and another here: Additionally, possibly related or not, the "created by" link in ...

Over-extended Affix in Bootstrap 3.1.0

I'm currently using Bootstrap 3.1.0. The issue I've encountered is that when the "affix" becomes too long for the viewport, it ends up getting cut off and doesn't display the bottom items. Is there a way to make Bootstrap's affix featu ...

Jquery fails to display errors or provide validation feedback

I have been struggling to implement form validation on my website without using jQuery validate. Unfortunately, the code I have written is not functioning properly and is causing some CSS issues in my local environment. My main objective is to validate fo ...

The CSS properties intended for ion-button elements are not taking effect

I'm attempting to set a background color when a ion-button is clicked or maintain the ion-ripple effect after it has filled the button in Ionic 4. I experimented with applying custom CSS states in global.scss, but encountered issues where the active ...

Adjust the height of column divs to equal the height of their parent container

Looking for a solution: I have multiple divs in a column layout and I want them to expand to match the original height of the parent. The parent's height is not fixed as it is part of a flex-based page design. Can this be done? In the example provid ...

Sending simple form information through AJAX to a PHP web service

Attempting to send form data via jQuery and Ajax to a PHP web service (php file) for echoing the results. This is my index.html file <html> <head> <title>PHP web service &amp; AJAX</title> <link rel="stylesheet" type="text/ ...

The order of CSS precedence shifts even when the class sequence is the same

In my development setup, I have a react component that is embedded within two distinct applications each with their own webpack configurations. Within the component, I am utilizing a FontAwesome icon using the react-fontawesome library: <FontAwesom ...

Problem with AJAX/HTTP communication

Looking for a solution to update link text dynamically based on tag count using AJAX? Take a look at this code snippet: <%= link_to "#{(@video.topics.count == 0) ? 'Add descriptive topics so people can find your song!' : 'Edit Topics&apo ...

The total sum of various divs containing a mix of numbers and letters

I have a group of div elements with the same class, each containing a value in the format (X1), (X2),... I need to add up these numeric values only, like 1 + 2 + .... Since these divs are generated dynamically, I won't know how many there will be. How ...

List of JQuery Elements

Being able to reference my JQuery selectors from an array instead of individually would be much more convenient. I thought that by using the following logic, I could achieve this: var reference = $('.class'); reference[0].<function>; As ...

Reliable drop-down box is not functioning properly

I am new to AJAX and I came across a solution for my issue on this page, but I am having trouble getting it to work. I want the input #precio to change when I select an option in the select #producto. This is the code I have: <form action="actualizar ...

Is the Site Header displayed depending on the scroll position and direction of scrolling?

On my website, I have a header that I want to hide when the user scrolls down 100px and show again when they scroll up 50px. I attempted to write a script for this functionality, but it doesn't seem to be working as expected. CSS /* This CSS rule w ...

Guide to displaying a template using Vue.js

Incorporating jQuery and vuejs version 2.3.4: https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js The code snippet below demonstrates my current setup: // Instantiating Vue. vueVar = new Vue({ el: '#content', ...

Include a lower border on the webview that's being shown

Currently, the webview I'm working with has horizontal scrolling similar to a book layout for displaying HTML content. To achieve this effect, I am utilizing the scroll function. My inquiry revolves around implementing a bottom border on each page usi ...