Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make div clickable with jquery

Tags:

jquery

jsfiddle: http://jsfiddle.net/FgZnK/1/

Clicking on the box goes to a page not found. How can I make this work?

HTML

<div class="myBox"></div>

jQuery

$(".myBox").click(function(){
    window.location=$(this).attr("http://google.com");
     return false;
});
like image 390
J82 Avatar asked Mar 11 '11 08:03

J82


People also ask

How do I make a div link 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.

How do I link a div in HTML?

By prepending your href with # , you can target an HTML element with a specific id attribute. For example, <a href="#footer"> will navigate to the <div id="footer"> within the same HTML document. This type of href is often used to navigate back to the top of the page.

How do you make a paragraph clickable in HTML?

The most important attribute that allows one to make links in HTML is the href attribute of the <a> element. As mentioned before, the href attribute indicated the link's destination. To break the code that helps you make text clickable in HTML and understand it better, <a href=” “> helps one to specify the target.


2 Answers

This is the correct code:

$(".myBox").click(function() {
    window.location = "http://google.com";
});
like image 119
Paweł Gościcki Avatar answered Sep 28 '22 22:09

Paweł Gościcki


http://jsfiddle.net/FgZnK/2/

HTML

<div class="myBox" data-href="http://google.com/">
</div>

JS

$(".myBox").click(function(){
    window.location = $(this).attr("data-href");
    return false;
});
like image 28
Alex Wayne Avatar answered Sep 28 '22 23:09

Alex Wayne