Parent and child hyperlinks both lead to new pages

I am facing an issue where a parent <a> with an href attribute contains a child <p>. My goal is to have a small box appear when the child element is clicked. However, the problem arises when clicking on the child element not only opens the small box but also triggers the parent link to open after a second. I do not want the parent link to open when clicking on the child element. Even after adding event.stopPropagation() and adjusting the z-index property, the issue persists.

You can view a live example in my JS Fiddle demo, but below is my current code:

.parent {
  background-color: blue;
  display: inline-block;
  width: 400px;
  height: 300px;
  z-index: 1;
}

.child {
  color: black;
  background-color: yellow;
  display: inline-block;
  width: 100px;
  height: 50px;
  z-index: 2;
}

[title] {
  position: relative;
  display: inline-flex;
  justify-content: center;
}

.child:focus::after {
  content: attr(title);
  position: absolute;
  top: 90%;
  color: #000;
  background-color: #fff;
  border: 1px solid;
  width: fit-content;
  padding: 3px;
  font-size: 10px;
  z-index: 20;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<a class="parent" href="https://www.google.com/">
  <p class="child" title="This is mobile tooltip" tabindex="0" (click)="$event.stopPropagation();">Click</p>
</a>

JS Fiddle demo

Note: Using jQuery is not an option for me.

Thank you.

Answer №1

Allow me to elucidate. What I accomplished was the creation of a variable that toggled whenever it hovered over or away from the element. Subsequently, when the user clicked on it, a conditional check was initiated to determine if the mouse cursor was not hovering over the element, and a specific code block was executed accordingly. If the cursor was indeed over the element, another code block was triggered.

var mouse = false;

function mouseStatus(n) {
  mouse = n;
}

function parent() {
  if (mouse == false) {
    console.log('parent');
    window.open('www.google.com');
  }
}

function child() {
  console.log('child');
}
.parent {
  background-color: blue;
  display: inline-block;
  width: 400px;
  height: 300px;
  z-index: 1;
}


.child {
  color: black;
  background-color: yellow;
  display: inline-block;
  width: 100px;
  height: 50px;
  z-index: 2;
}

[title] {
  position: relative;
  display: inline-flex;
  justify-content: center;
}

.child:focus::after {
  content: attr(title);
  position: absolute;
  top: 90%;
  color: #000;
  background-color: #fff;
  border: 1px solid;
  width: fit-content;
  padding: 3px;
  font-size: 10px;
  z-index: 20;
}
<a class="parent" onclick="parent()">
  <p class="child" title="This is mobile tooltip" onmouseover="mouseStatus(true);" onmouseout="mouseStatus(false);" onclick="child()">Click</p>
</a>

Answer №2

Instead of directly executing, consider calling a function :

handleEvent(event) {
   event.stopPropagation();
    return false;
}

To execute the function use:

(click) ="handleEvent($event)"

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

span element causing border-spacing problem

Is there a way to adjust the spacing between these borders? I've tried using border-spacing but it doesn't seem to be working. https://i.sstatic.net/ZY05g.png {{#each spacing}} <span class='space'> {{business}} ({{Count}}) < ...

Restrict the properties of an object to match the properties of a different object

I am currently developing an Object patching utility function with the following code snippet class Test{ a:number; b:number; } var c:Test={a:0,b:1} function patchable<T>(obj:T){ return { patch:function<K>(prop:K){ return patc ...

Encountering a proxy error while attempting to create an account or log in, with no network

Embarking on my first web development journey, I utilized React-Redux to craft a React.js application within the client folder. For the backend code, I employed Node.js and MongoDb as my database. This project represents a significant milestone in my lear ...

Exploring the wonders of looping through arrays with JavaScript and jQuery

I am having trouble getting this function to work properly. My goal is to replace certain characters when a key is pressed. The code works fine using the variable `replace_list` instead of `replace_list["russian"]`, but I actually need different "replace ...

Tips for moving directly to a section while maintaining a set distance from the top of the page

I'm currently implementing code for a smooth scroll to a section when clicking on a navigation menu item: // handling links with @href starting with '#' $(document).on('click', 'a[href^="#"]', function(e) { ...

Substitute the division

Can I make the old div change its position and move to the side when I add a new div? And can all the old divs be hidden when adding four new divs? This is for my full news website and I want this applied to all four pages. First page: https://i.sstatic. ...

Modify a property of an object using the useState Hook, where the property name is dynamically retrieved from a variable

const [fee, setFee] = useState({newPatient:'',establishedPatient:''}) const field1='newPatient' const field2='establishedPatient' To modify the fee object properties, I am looking to dynamically assign ...

I'm looking to convert this typescript function to return an array with strong typing instead of just a plain string[]

I am currently in the process of converting a JavaScript function to TypeScript. Originally, I believed that the type of the variable hi would be ('s'|'bb')[], but it turned out to be string[]. Is there a way for TypeScript to automatic ...

TypeScript typings for generic currying functions

I'm struggling to improve my skills in functional programming, particularly when dealing with typing generic "curry" functions. One example is a "Curry" version of the reduce function that I've written: const reduce = <S, R>(fn: (result: R ...

The JavaScript code does not call an external function to retrieve data from the database

I'm currently facing an issue with retrieving the same version data from my MySQL (MariaDB) Server. In order to streamline the process and improve maintenance, I decided to create a single connection Object to manage all database queries. However, I&a ...

Input new information into the Google Spreadsheet Database

I am currently working on updating and adding data to a Google spreadsheet that will serve as a database. Take a look at my spreadsheet here along with my JSfiddle link. While I have managed to retrieve information using the fiddle, I am facing challenges ...

Can the flat input structure be retained while using nestjs/graphql Args?

I was wondering about a simple query: @Query(() => ProfileOutput) async profile(@Args('id') id: string) { return await this.profileFacade.findProfileById(input.id); } My issue is that I'd like to use the @IsMongoId() validator from ...

Divide a cell table into multiple rows without distorting the table format

I am currently working on creating a screen layout that includes multiple rows and a column within a single cell. However, I'm facing issues with maintaining the formatting of my other table components. Although I have made some progress, whenever I ...

What is the most effective method for managing various types of single quotes (’) and (') in jQuery, Perl, and Mechanize?

Recently, I encountered an issue with a form in HTML that I'm submitting using jQuer.ajax to a Perl script which uses Mechanize to process the form on a specific URL. Everything seems to be working fine, except for one thing - when I check the informa ...

Validation of Forms and Displaying Error Messages in Angular 7

Please take a look at my straightforward form on Stackblitz https://stackblitz.com/edit/angular-et9uww Within my form, there are 2 checkboxes. When one checkbox is selected, it will populate a corresponding textbox. In this particular scenario, there are ...

Issue with Django Form not showing up on HTML after encountering an error

I have a question regarding my Django project. When a user sends a GET request, I need to display a form on the page. Here is the initial code snippet: form = SignUpForm() if request.method == 'POST': .... else: return render(request, & ...

The MUI Select component requires two clicks to open its menu if another Select component's menu is already open

I have been developing an application with two dropdowns (Select components) positioned next to each other, denoted as A and B. When A is open and the user intends to click on B to open it, I observed that in the default behavior of material UI, the user ...

Is it feasible to automatically fill out a separate form by clicking a link?

Seeking help with creating an Android app that can automatically populate the "RFC Emisor" and "RFC Receptor" fields on a specific web page: https://verificacfdi.facturaelectronica.sat.gob.mx. I believe the IDs for these inputs are: ctl00_MainContent_ ...

Is there a way to continuously iterate this PL/SQL statement in order to display numerous markers on my Google map?

In my current project, I am utilizing an Oracle 12c database, APEX 5, and the Google Maps API. What I am trying to achieve is plotting the coordinates of each sighting from the sighting table onto the map. To accomplish this, I prepare the map before ente ...

What is the best way to transform a ternary operation into an Enum Map with custom properties

I'm faced with the challenge of styling a button based on the selection made in a select box. The code snippet I have currently is as follows: const buttonStyles = { backgroundColor: buttonStyle === 'Black' ? colors.interactiveForeground ...