Quantcast
Channel: User Brett Merrifield - Stack Overflow
Viewing all articles
Browse latest Browse all 37

Linking a div to the href inside it

$
0
0

I want to make the whole staff-container linked to the first href inside of it. (The one in staff-picture). How do I write this with jQuery? I'm pretty confused with attr() and I know that I have to use it.

In the following HTML, the jQuery should make staff-container linked to google.com.

<div class="staff-container"><div class="staff"><div class="staff-picture"><a href="http://www.google.com/"><img src="img/people/teachers/ahmed.png" /></a></div><p><span class="bold">Mr. Ahmed</span><br />        Ext. 13417<br />        Room 417/323<br /><a href="mailto:email@wcskids.net">email@wcskids.net</a></p></div></div>

EDIT: Thanks to the answers I used this jQuery:

$('.staff-container').click(function() {    window.location.href = $(this).find('a').attr('href');});

If there is no link inside staff-picture I do NOT want it to link the email. If jQuery can't find the link in staff-picture, I want the click to do nothing.


Viewing all articles
Browse latest Browse all 37

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>