Skip to content
  • home
  • portfolio
  • web design journal
  • blog
    • adventure
    • san diego
    • stream
    • projects
    • website design
    • code notes
Blog Web Dev Making an Entire Div Clickable

Making an Entire Div Clickable

By Forrest Smith - Drempd.com

I wanted to make an entire div clickable — the destination for the click is derived from a link within the div that we give a class of ‘main_link’ to.

Insert the following jquery code into your page:

$(".stndBox").click(function(){
     window.location=$(this).find(".main_link").attr("href");
});

Your html code will look like the code below. The jquery will grab the href from the a tags with the class ‘main_link’. You can have additional links going to different locations, those will be ignored if they don’t have the ‘main_link’ class applied:

    
$(".stndBox").click(function(){ 				 
     window.location=$(this).find(".main_link").attr("href");  				 
});

Your html code will look like the code below. The jquery will grab the href from the a tags with the class ‘main_link’. You can have additional links going to different locations, those will be ignored if they don’t have the ‘main_link’ class applied:

<div class="stndBox">
     Div Content
     <a href="http://cnn.com" class="main_link">link</a>
</div>
<div class="stndBox">
     Div Content
     <a href="http://google.com" class="main_link">link</a>
</div>

I also changed the cursor, so while the mouse is over the clickable div, it all looks like a real link:

.stndBox{
     cursor: pointer
}
Posted on February 23, 2013August 26, 2022 in Web Dev

drempd | san diego, ca


home contact

adventures san diego projects stream website design

portfolio resume san diego web design freelance web designer

custom web design responsive web design website maintenance