make div clickable with jquery
This is the correct code:
$(".myBox").click(function() {
window.location = "http://google.com";
});
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;
});
In this chunk here:
$(".myBox").click(function(){
window.location=$(this).attr("http://google.com");
return false;
});
You're actually trying to read the non-existent attribute named http://google.com
.
Instead, just use:
$(".myBox").click(function(){
window.location = 'http://google.com';
});
If instead you want the actual destination to be in the mark up rather than the script, use:
$(".myBox").click(function(){
window.location = $(this).attr('href');
});
and put an href
attribute on your DIV, just like you would on an A link.
There's no need for return false
in any of these handlers because a DIV doesn't have a default action that needs preventing, unlike a normal link.