Create an event listener for clicking on an image that potentially has a corner ribbon obscuring a portion of it

In my project, there is a div element with an id of items containing several bootstrap cards. Each card includes an image tag and some are accompanied by the following HTML code:

<div class="ribbon"><span>Sale</span></div>

This code adds a CSS ribbon to the top right corner of the image, indicating that the item is on sale.

I have implemented a jQuery script using the on("click","img",function() function:

$("#items").on("click", "img", function () {
    var path = $(this).attr('alt');
    if (path != "none") {
        $('#model-image-img').attr('src', path);
        $('#model-title').text($(this).attr('data-title'));
        $('#model-image').modal('show');
    } else {
        return false;
    }

This script opens a modal displaying a larger version of the image when it is clicked.

However, I encountered an issue where clicking on the ribbon in the top right corner of the image does not trigger the modal to open. How can I modify the behavior so that clicking on the ribbon also opens the modal to display the larger image?

Answer №1

To start, create a container named <div class="special">. Place your image and ribbon elements inside this special div. After that, update the handler

$("#items").on("click", "div.special", function () {
    var path = $(this).find("img").attr('alt');
...

to ensure that the click event is triggered from within the special div.

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

Bringing HTML into Excel with VSTO

I have encountered an issue while trying to import data from HTML files into Excel using VSTO (VB.NET). I am facing errors as I attempt to parse the HTML, and since I am new to this process, I need assistance in figuring out where my code is incorrect. I ...

Is it possible for me to transform this code into a useful helper function?

I am looking to optimize this conditional code by converting it into a helper function using a switch statement instead of multiple if statements. How can I achieve this in a separate file and then import it back into my component seamlessly? import { ...

what is the advantage of utilizing negative margins?

Examining some CSS code and came across this: .head{ position:relative; overflow:hidden; margin:-30px 0 0 -25px; width:820px; padding:20px 25px 0 25px; background:url(/images/bkg.gif) 0 0 no-repeat; } What's the reason behind using -30px and -25px m ...

Using <span> tags to wrap sentences within <p> tags while maintaining the rest of the HTML formatting

In order to achieve my goal, I have utilized the following code to extract content within tags and encapsulate each sentence in tags for easier interaction. $('p').each(function() { var sentences = $(this) .text() ...

Error encountered with Node.js clustering

Hey there! I'm currently diving into the world of node.js and javascript. My goal is to build a cluster.js using the nodejs cluster module, where at the end of my if statement I invoke server.js to kickstart the application. Here's my cluster.js ...

What is the best way to access the image source attribute within a directive?

Here's a straightforward image directive example: (function () { 'use strict'; angular .module('myapp') .directive('imageTest', imageTest); function imageTest() { var directive = { ...

Executing a function enclosed in parenthesis does not yield any output

My code is supposed to print the sender's name followed by "adopted" and then the first mentioned user. const { Client } = require('discord.js', 'async', 'discord-message-handler'); const bot = new Client(); const cfg = ...

How can parameters be sent without using the .jshintrc file with the gulp-jshint package?

Currently in the process of transitioning a grunt project to a gulp project. In the existing gruntfile, there is a section for the jshint task that looks like this: jshint: { options: { trailing:true, evil:false, indent:4, ...

The image link leading to the homepage lacks the functionality to be clicked on

My website has a logo on the top left that links to the home page, but only some areas of the image are clickable. It seems like the clickable and non-clickable areas are scattered randomly. How can I ensure that the link works across the entire area of ...

Javascript continuously swaps out text from distinct strings in a loop

I wrote a loop to split a string and search for certain text to replace it with another string. Here is an example of what I have done: function replaceStr(str, find, replace) { for (var i = 0; i < find.length; i++) { str = str.replace(new RegE ...

designing a unique organizational chart layout with HTML and CSS

I'm facing a challenge in creating a customized organizational chart layout using HTML and CSS. Despite my efforts, I am having difficulties achieving the desired structure that includes parent, child, and grandchild nodes connected in a specific way. ...

Sharing data between promises in Node.jsExplanation: In Node.js, passing values

Can someone assist with passing an object that I am creating step by step using promises with firebase? I would like to know if there is a more efficient way to construct the object without passing it through the promise chain. Below is the code snippet I ...

Encountering a Syntax Error within the ng-click function of a button in AngularJS

I'm currently developing a mobile-based web application using angular js along with onsen ui. I've created a follow button on the user's profile page, allowing users to follow each other. However, when the user's profile loads and the a ...

Remove all of the classes from the specified element using JavaScript

I have a button and I want to remove all the classes when it is clicked. Here is what I have tried: button.style.className = '' document.querySelector('button').onclick = function() { this.style.className = ''; } .myClas ...

I am experiencing issues with my React DND implementation in Next JS - can anyone help troubleshoot?

I'm encountering an issue with my React DND implementation. Although I am able to drag elements, they are not being received by the drop targets. I even tried using console.log in the drop function of useDrop but nothing is logging in the console on d ...

Is there a way to customize the checkout page using JavaScript?

I'm working on a checkout page where fields need to change based on a selected radio button. There are two options: A and B. When A is selected, I want certain fields to remain visible while others disappear, and vice versa for option B. Although I p ...

Animating text-shadow color in CSS from left to right

I need help with transitioning the text-shadow color horizontally on hover. I found a solution that demonstrates how to do this here: Although it shouldn't matter, I am working in react with styled-components. Here is an example of what I am trying ...

Create an additional column in HTML using Bootstrap styling

I am currently working on a CRUD form (using PHPMyAdmin for the database), developed in HTML, JavaScript, and PHP. The queries are executed using jQuery. I am facing an issue with my code - whenever I add a new column to my table, the formatting gets messe ...

Utilize conditional styling along with hover effects in ReactJS

I'm facing an issue where I need to set up a conditional style and a hover effect for TableRows. They work fine individually, but when combined, the hover function stops working. CSS const StyledTableRow = withStyles((theme) => ({ root: { tab ...

What causes pagination to be displayed outside of the main section in Firefox when using swiper?

When using swiper, why does pagination display outside of the main section in Firefox when inserting slides with different content lengths? This issue seems to only occur in Firefox and not in other browsers like Chrome. I have noticed that using two boots ...