JQuery clickable div with working mailto link inside

I have a div that I want to click, but I need a mailto link inside this div to still work. When I hover over the mailto link, mailto appears at the bottom of the browser, but clicking activates the link attached to the div. Anyway, around this? Thanks.

<div class="directory_search_results"> <img src="images/no_photo.png" /> <ul class="staff_details"> <li class="search_results_name"><a href="http://www.netflix.com">Micheal Staff</a></li> <li class="search_results_location">University of Illinois</li> <li class="search_results_email"><a href="mailto: test@test.org "> test@test.com </a></li> <li class="search_results_phone">(407) 555-1212</li> <li class="search_results_office">(407) 555-1212</li> </ul></div> 

And now jQuery

 $(document).ready(function(){ $(".directory_search_results").click(function(){ window.location=$(this).find("a:first-child").attr("href"); return false; }); 
+4
source share
2 answers

Add a class to your mailto: link as follows:

 <div class="directory_search_results"> <img src="images/no_photo.png" /> <ul class="staff_details"> <li class="search_results_name"><a href="http://www.netflix.com">Micheal Staff</a></li> <li class="search_results_location">University of Illinois</li> <li class="search_results_email"><a class="nobubble" href="mailto: test@test.org "> test@test.com </a></li> <li class="search_results_phone">(407) 555-1212</li> <li class="search_results_office">(407) 555-1212</li> </ul></div> 

And add the following jQuery code

 $('.nobubble').click(function(event){ event.stopImmediatePropagation(); }); 

This will prevent event bubbles from appearing before the parent div ant triggers a click of a button.

+9
source

Consider this.

one of the elements jquery offers is a modal dialog.

Basically, it provides a β€œscreen” dialog box that sits above the rest of the screen so that you don't click on anything else on the page during the dialog.

div ... which is an element of a container, can contain several elements ... any element inside a div is still considered part of the div.

Now I'm not a professional when it comes to the structure and hierarchy of the DOM, but it seems to me that what you are trying to do will not work.

if there is something like a link inside a div, and jquery looks at the click event of this element. I would suggest that anything inside the div would be β€œbelow” the screen provided by the div. so if you have a link inside a div and you click on the link, you also technically click on the div as well.

0
source

Source: https://habr.com/ru/post/1340224/


All Articles