Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a whole div clickable

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...

like image 359
King Julien Avatar asked Jul 20 '10 17:07

King Julien


People also ask

How do I make a full div clickable?

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.

Can an entire div be a link?

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.

Can you add onclick to a div?

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.

How do you hyperlink a div in HTML?

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.


2 Answers

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.

like image 65
Sarfraz Avatar answered Oct 06 '22 01:10

Sarfraz


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/

like image 23
Hbirjand Avatar answered Oct 06 '22 00:10

Hbirjand