Select various icons within a div that already has a click event attached to it

Having an issue with a clickable div element.

Working on an Ionic 2 audio application where I have a series of divs each containing different icons.

Specifically, each div has two icons - one for downloading the audio and the other for playing it.

I want to be able to click on each icon and have the appropriate action take place. However, if I click on any other part of the div besides the icons, it should default to playing the audio.

The problem arises when I attach the click event to the parent div, as it then overrides the events of the child icons. This results in the audio playing every time I click on the download or play icon, even though I intended to click on the parent div.

Below is the code snippet:

<div class="audioItem" ion-item *ngFor="let audio of audios" (click)="tryToPlay(audio)">
            <img id="leftIcon" src="img/audio-icon.png">
            <h3>{{ audio.name }}</h3>
            <div *ngIf="audio.downloaded">
                <img class="downloadIcon" src="img/download-icon.png" (click)="clicked(audio)">
            </div>
            <div *ngIf="!audio.downloaded">
                <img class="downloadIcon" src="img/not-downloaded-icon.png" (click)="clicked(audio)">
            </div>
            <div id="play" *ngIf="!audio.locked">
                <img id="arrowIcon" src="img/play-icon.png" (click)="tryToPlay(audio)">
            </div>
            <div *ngIf="audio.locked">
                <img id="lockIcon" src="img/lock-icon.png" (click)="tryToPlay(audio)">
            </div>
</div>

Keep in mind that the clicked() method is just for testing purposes, it could be any other method like "download()". The main focus is on enabling the click events for the child icons.

Answer №1

If you encounter this issue, one way to resolve it is by incorporating event.stopPropagation();.

For further guidance, refer to this plunker.

As demonstrated in the example provided, you have the option to pass the $event object to both functions

<ion-list>
    <ion-item *ngFor="let item of items" (click)="open($event, item)">
        {{ item }}
        <ion-icon (click)="download($event, item)" item-right name="download"></ion-icon>
    </ion-item>
</ion-list>

Subsequently, utilize this data to prevent event propagation, ensuring only the intended function is executed

public open(event, item) {
    event.stopPropagation();
    alert('Open ' + item);
  }

  public download(event, item) {
    event.stopPropagation();
    alert('Download ' + item);
  }

Answer №2

Here is my solution:

<ion-list>
        <div class="audioContainer" *ngFor="let audio of videos">
            <div class="audioItem" ion-item (click)="playAudio(audio)">
                <img id="leftIcon" src="img/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe9f8b9a9791d3939f999b908a9fbecdcece86d08d8899">[email protected]</a>">
                <h3>{{ audio.title }}</h3>
            </div>
            <div *ngIf="audio.downloaded">
                <img class="downloadIcon" src="img/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="395d564e575556585d1456575a55505a52790a090941174a4f5e">[email protected]</a>" (click)="downloaded(audio)">
            </div>
            <div *ngIf="!audio.downloaded">
                <img class="downloadIcon" src="img/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96f2f9e1f8faf9f7f2d6a5a6a6eeb8e5e0f1">[email protected]</a>" (click)="downloaded(audio)">
            </div>
            <div id="play" *ngIf="!audio.locked">
                <img id="arrowIcon" src="img/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98e8f4f9e1d8aba8a8e0b6ebeeff">[email protected]</a>" (click)="playAudio(audio)">
            </div>
            <div *ngIf="audio.locked">
                <img id="lockIcon" src="img/ui-lock100.png" (click)="playAudio(audio)">
            </div>
        </div>
</ion-list>

Custom CSS:

.audioContainer {
    position: relative;
}

ion-list .audioItem {
    position: relative;
    color: #fff !important;
    margin-bottom: .1em !important;
    padding-top: .5em !important;
    padding-bottom: .5em !important;
}

ion-list .audioItem img#leftIcon {
    width: 9% !important;
    float: left;
    margin-right: 0;
}

ion-list img#arrowIcon {
    position: absolute;
    right: 4%;
    bottom: 22%;
    width: 10%;
}
ion-list img#lockIcon {
    position: absolute;
    right: 1.3em;
    bottom: 1.6em;
    width: 3%;
}

ion-list img.downloadIcon {
    position: absolute;
    width: 10%;
    right: 15%;
    bottom: 22%;
}

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

Refreshing Your WordPress Post Automatically

Can someone assist me with setting up an autorefresh feature for a specific div that has the ID of "blue"? I want the div to automatically update whenever I make changes to a post with that same ID. var auto_refresh = setInterval( function () { $('#b ...

Tips for efficiently managing angular route subscriptions and leveraging parameters for simultaneous http requests

I am seeking the best solution for the following situation: My goal is to trigger two parallel http requests when an Observable from the route parameters subscription fires. I attempted to use switchMap in combination with forkJoin, but unfortunately it ...

What steps are involved in constructing Jodit from scratch?

Seeking a non-minified, readable version of Jodit, I attempted to build it myself. However, my lack of experience with composer, node, npm, webpack, TypeScript, and other tools has left me struggling. Is there anyone who can guide me through the process s ...

What is the best way to ensure that my background image remains fully covering the screen even while scrolling or resizing

Struggling with implementing parallax scrolling through multiple images. Encountering two main issues: 1) Tiling occurs with repeated images or incomplete display, and 2) Inconsistent resizing on different screen sizes, particularly mobile phones. Any s ...

Enhance your web design with the mesmerizing jQuery UI drop effect combined

I'm attempting to create an animated toggle effect on a box using jQuery UI's drop feature. However, I've encountered some trouble due to the box having box-sizing: border-box applied which is causing issues with the animation. During the a ...

Conceal the div containing all its content within

I need to hide a div with all the content inside it using this code: <form action="" method="POST" name="form"> <p for="name">text :</p><input type="text" name="name" value="<?php echo $name_g; ?>" onfocus="ClearPlaceHolder (thi ...

Position the colored div on the left side of the page

Here is the CSS I am currently using... <style type="text/css"> .widediv{width:1366px;margin-left:auto;margin-right:auto} </style> This CSS code helps me create my webpage : <body><div class="widedivv"> <!-- Content go ...

Utilize JavaScript and jQuery to extract the selected text's context from a webpage

I'm looking to extract the contextual information surrounding a selected text on a web page, specifically the 25 words before and after it. I've tried using JavaScript and jQuery with the following code snippet but unfortunately, it doesn't ...

Exploring the implementation of query parameters in Nest.js

I am currently a freshman in the world of Nest.js. Below is an excerpt from my code: @Get('findByFilter/:params') async findByFilter(@Query() query): Promise<Article[]> { } I have utilized Postman to test this specific router. ht ...

Using jQuery, Ajax, and HTML together can create dynamic

Is there a way to run JavaScript functions in an HTML file that is loaded using AJAX? The HTML file contains both text and JavaScript, but only the inline JavaScript seems to work (like onclick="dosomething();return false"). The pre-defined functions wra ...

Transforming JSON data into XML using Angular 7

It turns out the xml2js npm package I was using doesn't support converting JSON to XML, which is exactly what I need for my API that communicates with an application only accepting XML format. In my service file shipment.service.ts import { Injecta ...

Navigate to the previous or next page using JavaScript

I've been struggling to figure out how to navigate one page up and down using simple links, but I haven't found a solution that works for me. The URL I am working with is: . When the "next page" button is clicked, it should go to ?page=2, and whe ...

What's the best way to add vertical space to my DIV containing buttons?

Here is an example of HTML with CSS classes that float elements left and right: <div class="block-footer"> <button class="medium float-left">A</button> <button class="medium float-left">A</button> <button class="m ...

What is the best way to apply typography theme defaults to standard tags using Material-UI?

After reading the documentation on https://material-ui.com/style/typography/ and loading the Roboto font, I expected a simple component like this: const theme = createMuiTheme(); const App = () => { return ( <MuiThemeProvider theme={theme}> ...

A guide on efficiently storing and retrieving a webpage containing two angular2 components using local storage

I've been attempting to store and retrieve a page containing two angular2 components from local storage using the following code, but the component CSS is not being applied. Here is the code I'm using to save: localStorage.setItem('pageCon ...

Utilizing Angular 2's Custom Control to link with ngControl using the Control Value Accessor, along with the integration of

I'm currently working on implementing a combo date picker in Angular 2 that associates a date with three dropdown lists - one for day, one for month, and one for year. I found a helpful article that I've been using as a reference: http://almero ...

Hidden background image not shown

While working on a component in vue.js, I encountered an issue where the image is not being displayed within the specified tag: <b-icon v-if="displayShowHistory && checkRole('showHistory')" class="backlight show-modal-ic ...

Leveraging interfaces with the logical OR operator

Imagine a scenario where we have a slider component with an Input that can accept either Products or Teasers. public productsWithTeasers: (Product | Teaser)[]; When attempting to iterate through this array, an error is thrown in VS Code. <div *ngFor= ...

Enhance your React Highchart by incorporating gradient shading to the data points

One interesting feature in classic highcharts is the ability to apply gradient coloring to points: Highcharts.setOptions({ colors: Highcharts.getOptions().colors.map(function (color) { return { radialGradient: { cx: ...

Why is my CSS causing a div with 100% width to be slightly taller than expected?

I am working on a website that I want to adjust to the width of different browsers. Everything seems to be functioning properly, except for the fact that the parent div containing the 100% width divs always appears around 5 pixels too tall. This is causin ...