How do you make a div tag into a link. I would like my entire div to be clickable.
10 Answers
JS:
<div onclick="location.href='url'">content</div>
jQuery:
$("div").click(function(){
window.location=$(this).find("a").attr("href"); return false;
});
Make sure to use cursor:pointer
for these DIVs

- 66,048
- 104
- 272
- 428
-
3If I didn't understand the jQuery sample wrongly, the HTML have to be structured this way: – Seh Hui Leong Nov 06 '09 at 02:49
-
Yes, and thats probably a better way to do it. I just posted it as an alternative. – eozzy Nov 06 '09 at 06:08
If you're using HTML5, as pointed in this other question, you can put your div
inside a
:
<a href="http://www.google.com"><div>Some content here</div></a>
Preffer this method as it makes clear in your structure that all the content is clickable, and where it's pointing.

- 1
- 1

- 3,327
- 30
- 25
-
It will work fine but if you are more concern on the SEO purpose and performance these method is not that much good. Because According to semantic syntax we cannot put block level element into inline element. So Please let me know the correct answer. – Nadeem Khan Nov 12 '21 at 13:12
-
2@NadeemKhan Starting with HTML5, anchor can contain other elements. Check this [other question](https://stackoverflow.com/questions/3379392/what-elements-can-be-contained-within-a-a-tag), referring to the anchor changes in documentation: http://w3c.github.io/html-reference/a.html#a-changes – Rael Gugelmin Cunha Nov 12 '21 at 17:41
-
https://clearlydecoded.com/html-content-models Please go to the above link and read the error about putting block level element into inline element. I am dam sure that it would affect on SEO as well because according to content model these is not correct if you want to make good website then we have to take care of these thing. Are you agree with my points ? – Nadeem Khan Nov 13 '21 at 18:22
-
1I gave you the official HTML 5 documentation link, a StackOverflow explanation about it, telling you that your point is obsolete (it was valid in HTML4 days). Any good web crawler will just skip most of tags and parse links and the text content. – Rael Gugelmin Cunha Nov 15 '21 at 13:12
If you have to set your anchor tag inside the div, you can also use CSS to set the anchor to fill the div via display:block.
As such:
<div style="height: 80px"><a href="#" style="display: block">Text</a></div>
Now when the user floats their cursor in that div the anchor tag will fill the div.

- 2,340
- 19
- 31
-
1In Firefox 3.5 and IE8, I have to add "height: 100%;" to the anchor tag for it to take up the whole DIV height. Great alternative to the Javascript solutions. – Seh Hui Leong Nov 06 '09 at 04:41
-
1
So you want an element to be something it's not?
Generally speaking this isn't a good idea. If you need a link, use a link. Most of the time it's easier to just use the appropriate markup where it belongs.
That all said, sometimes you just have to break the rules. Now, the question doesn't have javascript, so I'm going to put the disclaimer here:
You can't have a <div>
act as a link without either using a link (or equivalent, such as a <form>
that only contains a submit button) or using JavaScript.
From here on out, this answer is going to assume that JavaScript is allowed, and furthermore that jQuery is being used (for brevity of example).
With that all said, lets dig into what makes a link a link.
Links are generally elements that you click on so that they navigate you to a new document.
It seems simple enough. Listen for a click event and change the location:
Don't do this$('.link').on('click', function () {
window.location = 'http://example.com';
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="link">Fake Link</div>
There you have it, the <div>
is now a link. Wait...what's that? What about accessibility? Oh right, screen readers and users of assistive technology won't be able to click on the link, especially if they're only using the keyboard.
Fixing that's pretty simple, let's allow keyboard only users to focus the <div>
, and trigger the click event when they press Enter:
$('.link').on({
'click': function () {
window.location = 'http://example.com';
},
'keydown': function (e) {
if (e.which === 13) {
$(this).trigger('click');
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="link" tabindex="0">Fake Link</div>
Again, there you have it, this <div>
is now a link. Wait...again? Still accessibility problems? Oh ok, so it turns out that the assistive technology doesn't know that the <div>
is a link yet, so even though you can get there via keyboard, users aren't being told what to do with it.
Fortunately, there's an attribute that can be used to override an HTML element's default role, so that screen readers and the like know how to categorize customized elements, like our <div>
here. The attribute is of course the [role]
attribute, and it nicely tells screen readers that our <div>
is a link:
$('[role="link"]').on({
'click': function () {
window.location = 'http://example.com';
},
'keydown': function (e) {
if (e.which === 13) {
$(this).trigger('click');
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div role="link" tabindex="0">Fake Link</div>
Finally, our <div>
is a lin---oh now the other devs are complaining. What now?
Ok, so the devs don't like the code. They tried to preventDefault
on the event, and it just keeps working. That's easy to fix:
$(document).on({
'click': function (e) {
if (!e.isDefaultPrevented()) {
window.location = 'http://example.com';
}
},
'keydown': function (e) {
if (e.which === 13 && !e.isDefaultPrevented()) {
$(this).trigger('click');
}
}
}, '[role="link"]');
$('[aria-disabled="true"]').on('click', function (e) {
e.preventDefault();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div role="link" tabindex="0">Fake Link</div>
<div role="link" aria-disabled="true" tabindex="0">Fake disabled link</div>
There we have it---THERE'S MORE? What else don't I know? Tell me everything NOW so that I can fix it!
- Ok, so there's no way to specify target. We can fix that by updating to
window.open
. - Click events and keyboard events are ignoring Ctrl, Alt, and Shift keys. That's easy enough, just need to check those values on the event object.
- There's no way to specify contextual data. Let's just add some
[data-*]
attributes, and call it a day with that one. - The click event isn't obeying the mouse button that's being used, middle mouse should open in a new tab, right mouse shouldn't be triggering the event. Easy enough, just add some more checks to the event listeners.
- The styles look weird. WELL OF COURSE THE STYLES ARE WEIRD, IT'S A
<DIV>
NOT AN ANCHOR!
well, I'll address the first four issues, and NO MORE. I've had it with this stupid custom element garbage. I should have just used an <a>
element from the beginning.
$(document).on({
'click': function (e) {
var target,
href;
if (!e.isDefaultPrevented() && (e.which === 1 || e.which === 2)) {
target = $(this).data('target') || '_self';
href = $(this).data('href');
if (e.ctrlKey || e.shiftKey || e.which === 2) {
target = '_blank'; //close enough
}
open(href, target);
}
},
'keydown': function (e) {
if (e.which === 13 && !e.isDefaultPrevented()) {
$(this).trigger({
type: 'click',
ctrlKey: e.ctrlKey,
altKey: e.altKey,
shiftKey: e.shiftKey
});
}
}
}, '[role="link"]');
$('[aria-disabled="true"]').on('click', function (e) {
e.preventDefault();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div role="link" tabindex="0" data-href="http://example.com/">Fake Link</div>
<div role="link" tabindex="0" data-href="http://example.com/" data-target="_blank">Fake Link With Target</div>
<div role="link" aria-disabled="true" tabindex="0" data-href="http://example.com/">Fake disabled link</div>
Note that stack snippets won't open popup windows because of how they're sandboxed.
That's it. That's the end of this rabbit hole. All of that craziness when you could have simply had:
<a href="http://example.com/">
...your markup here...
</a>
The code I posted here probably has problems. It probably has bugs that even I don't realize as of yet. Trying to duplicate what browsers give you for free is tough. There are so many nuances that are easy to overlook that it's simply not worth trying to emulate it 99% of the time.

- 174,988
- 54
- 320
- 367
DON'T DO IT.
- If you want a link, wrap the content in the proper
<A>NCHOR</a>
. - If you want to turn the
<DIV>
into a link, use "Javascript" to wrap the<DIV>
inside an<A>NCHOR</A>
- If you want to perform some action when clicking the
<DIV>
use theonclick
event handler... and don't call it a "link".
<div style="cursor:pointer" onclick="document.location='http://www.google.com'">Content Goes Here</div>

- 3
- 3

- 8,284
- 5
- 33
- 47
<div style="cursor:pointer;" onclick="document.location='http://www.google.com'">Foo</div>

- 8,042
- 3
- 35
- 37
-
1Consider adding an explanation to your answer to help keep SO a resource for learning and not just answers. – Jon Egeland Aug 29 '12 at 00:24
You can make the entire DIV function as a link by adding an onclick="window.location='TARGET URL'" and by setting its style to "cursor:pointer". But it's often a bad idea to do this because search engines won't be able to follow the resulting link, readers won't be able to open in tabs or copy the link location, etc. Instead, you can create a regular anchor tag and then set its style to display:block , and then style this as you would a DIV.

- 10,058
- 2
- 33
- 44
You could use Javascript to achieve this effect. If you use a framework this sort of thing becomes quite simple. Here is an example in jQuery:
$('div#id').click(function (e) {
// Do whatever you want
});
This solution has the distinct advantage of keeping the logic not in your markup.

- 584
- 3
- 12