I have a link inside a div and I need to make the whole div clickable... found several tutorials on the Internet but non of them worked for me...
We simply add the onlcick event and add a location to it. Then, additionally and optionally, we add a cursor: pointer to indicate to the user the div is clickable. This will make the whole div clickable.
simple answer is no, you can use onclick with css cursor:pointer to get the same functionality, though. Show activity on this post. Per the HTML spec (HTML 4.01, Draft 5 and XHTML 1,1.1) an anchor tag <a> cannot enclose a <div> tag. Since this answer was posted, HTML5 has come out.
We can bind a JavaScript function to a div using the onclick event handler in the HTML or attaching the event handler in JavaScript. Let us refer to the following code in which we attach the event handler to a div element. The div element does not accept any click events by default.
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.
Raw JavaScript:
<div onclick="alert('You clicked me !')">Click Me</div>
jQuery:
$('#div_id').click(function(){
alert('Clicked !!');
});
Update: (With reference to your link)
<div class="myBox">
blah blah blah.
<a href="http://google.com">link</a>
</div>
jQuery:
$(".myBox").click(function(){
window.location=$(this).find("a").attr("href");
return false;
});
The above code cancels the default action of link (going to link) with return false
and binds the click
event to the div with class myBox
, then it finds the link's src
attribute inside the div and window.location
is used to redirect the page to the src
attribute of the link present inside the div. So this basically makes the div clickable.
You can use a JavaScript code at to achieve your goal, please take a look at this tutorial.
$(".myBox").click(function(){
window.location=$(this).find("a").attr("href");
return false;
});
and this is the HTML example :
<div class="myBox">
blah blah blah.
<a href="http://google.com">link</a></div>
but there is a tricky way to achieve this using a CSS code you must nest an anchor tag inside your div tag and you must apply this property to it,
display:block;
when you've done that,it will make the whole width area clickable (but within the height of the anchor tag),if you want to cover the whole div area you must set the height of the anchor tag exactly to the height of the div tag,for example:
height:60px;
this is gonna make the whole area clickable,then you can apply text-indent:-9999px
to anchor tag to achieve the goal.
this is really tricky and simple and it's just created using CSS code.
here is an example: http://jsfiddle.net/hbirjand/RG8wW/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With