Make a div clickable
We all dislike that links are so small, and hard to click. So of course we want to make the clickable areas bigger. Some would think that doing this with some CSS on the a-tag would be a good way, but then you can't have block level elements inside it (you'll get a validation error if you try to put headings or paragraph tags inside of links). So what's a more general solution?
My take is to use a div instead, and use javascript, of course together with a good fallback. When clicking the div, find the first link inside it, and go to that URL. It's simple, and with a few additional quirks, it gets really useful.
Javascript code (requires jQuery):
$(document).ready(function(){
var block = $(".teaser");
block.click(function(){
window.location = $(this).find("a:first").attr("href")
});
block.addClass("clickable");
block.hover(function(){
window.status = $(this).find("a:first").attr("href")
}, function(){
window.status = ""
})
});
CSS for showing that the div actually is clickable:
.clickable {
cursor: pointer;
}
.clickable:hover {
background: #efefef;
}
A clickable div demo is of course available.
How it works#
- When the div (or other tag, you choose) is clicked, find the first anchor tag, get it's href attribute, and go there. Relying on an actual link means you always have a fallback, so no cheating.
- When javascript is disabled, it just falls back to a regular block, where only the links are clickable.
- A class called "clickable" is set on the block to allow for javascript-specific styling, such as changing the cursor with cursor: pointer, something you don't want to happen when the block isn't clickable.
- The changing background color on hover is done with CSS, which I think is fair, considering the small percentage of users using IE6. Changing background color isn't a must feature.
- Lastly, since we're simulating a link here, it should show where the link is going. I've done this by setting the statusbar to the link location on hover, something that's useful when it works (users can disable this in some browsers).
Hope that little snippet is useful for someone out there, I think it's a good example of good use of javascript.
Comments
By: Ole (#1)
Thanks for that, i'll try this out.
By: grimen (#2)
(function($)
{
$.fn.clickable = function(options)
{
var clickable = this;
clickable.each(function()
{
var e = $(this);
e.click(function()
{
window.location = $(this).find('a:first').attr('href')
});
e.hover(function()
{
window.status = $(this).find('a:first').attr('href')
}, function()
{
window.status = ''
});
e.addClass('anchor');
});
return clickable;
};
})(jQuery);
By: trovster (#3)
By: Zach Leatherman (#4)
James Padosley had a good method for this:
http://james.padolsey.com/javascript/table-rows-as-clickable-anchors/
By: Emil Stenström (#5)
By: Anon (#6)
By: Emil Stenström (#7)
@Anon: What kind of confusion? These are all extra features, that make things easier for the user. If you look at the example, you'll see that more padding just doesn't work. Bigger clickable area (which the design should reflect, of course), means easier for user to hit it. I see only good things.
By: Krijn Hoetmer (#8)
a
element.By: Emil Stenström (#9)
By: Siegfried (#10)
The better solution i think :)
By: Emil Stenström (#11)
By: Krijn Hoetmer (#12)
display: block;
on your links (and make sure to use closingp
tags, etc.), so it's one of the safe parts of the draft you can already use, imho. The only problem this doesn't solve is making entiretr
elementshref
able, due to table parsing and the magic that involves. You still need JavaScript for that.By: Krijn Hoetmer (#13)
:hover
styles for free (without using JS hacks) in IE6, since that babe only understands:hover
ona[href]
.I only wonder how search engines and AT work with this. "Click here" links clearly suck, but perhaps linking entire blocks of text, including headings, sucks as well. Would using
title=""
on the link solve that? Somebody should do some study in that field :)By: Emil Stenström (#14)
By: Anders Ytterström (#15)
That will save a few buck on DOMready, especially in IE which is really slow in comparison.
By: Stevie D (#16)
onClick
event to the containingdiv
- see website link for example. But this looks like a better solution, means I don't need to worry about putting in the links twice every time.It depends on the nature of the content. In some cases, extra padding, leading or spacing will do a good job. In other cases, it helps to make surrounding content clickable. For example, on my website, there is a 'panel' that contains several items, and making the entire panel into a link makes it easier for users to select it - while, of course, retaining on old fasioned anchor on the first line for accessibility and spiders.
By applying
:hover
properties to thediv
, you give the visual affordance of a link to users (hand cursor, change of colour), so there should be no problem with unexpected behaviour.By: Emil Stenström (#17)
By: Andreas (#18)
I've done it this way before though as well, just a thought.
Regarding the JS-only CSS; I've used a js-enabled/js-disabled class on the body-element for ages now and it's perfect for things like this.
I add the js-enabled class immediately after the opening body-tag so there'll be no lag for the JS-only styling:
<body class="js-disabled">
<script type="text/javascript">
document.body.className = 'js-enabled';
</script>
As any performance-aware front-end developer I include all my JS before the _closing_ body-tag so it normally takes a couple of seconds before it kicks in. Giving body the js-enabled class at the top avoids styles blinking in though.
By: Emil Stenström (#19)
Nah, I still like the HTML5 solution (see above comment) best I think.
By: Alex (#20)
Anyway the demo looks great!
By: Emil Stenström (#21)
By: Alex (#22)
By: Emil Stenström (#23)
By: Alex (#24)
What i really like is that even if javascript is disabled you don't get any error messages and the div block remains as if there is no javascript there.
By: Hacer un div clicable | Artimedia Blog (#25)
By: Lewis Litanzios (#26)
I used this typa technique recently on a 'Read more...' link: http://is.gd/4n09N (pulling in the post title URL and then hiding the title).
By: Kristopher (#27)
By: Sheena Rai (#28)
By: Mac (#29)
1)How do I change the code for the 3 divs?
2)Where does the script actually go?
Sorry for the simple questions, I'm not a programmer!
By: Emil Stenström (#30)
For a working example, and descriptions of where the script goes, check out the demo page, and copy code from there.
By: andy (#31)
By: andy (#32)
By: Robin Parduez (#33)