Welcome to %s forums

<No. 1 Community for developers, by developers/>

Login Register

How to make entire div clickable and behave link anchor?

Get support on JavaScript,jQuery, AJAX and other related technology.
Post Reply
admin
Site Admin
Posts: 45

How to make entire div clickable and behave link anchor?

Post by admin » Thu Aug 22, 2019 2:52 pm

HTML:

Code: Select all

<div class="myContent">
  This is dummy text
  <a href="http://google.com">link</a>
</div>
jQuery:

Code: Select all

window.location = $(this).data("location");
Full Code:

Code: Select all

$(".myContent").click(function() {
  window.location = $(this).find("a").attr("href"); 
  return false;
});
Important:
If your website is running on HTML5 then you need not have to rely upon above methods, you can simply follow below markup:

Code: Select all

<a href="http://example.com">
  <div>
     anything
  </div>
</a>
Here you can notice, HTML5 allows us to use div tag inside an anchor element.

Post Reply