-4

I have this markup to make the entire div clickable.

<div class="myBox">
<div class="two">
    <h2> Title</h2>
    <p> This is the description</p>
    <a class="some-button-class" href="http://www.google.com/">check here</a>
</div>
</div>

And this JS

 $(".myBox").click(function(){
 window.location=$(this).find("a").attr("href"); 
 return false;
});

And this css

.myBox { cursor:pointer;width:200px;height:200px;}
h2 {font-size:28px;}
p { color:#fff }

.two { 
background: #999;
padding:50px;
}

But it's not working. Why?!

JSfiddle http://jsfiddle.net/ryRnU/16/

SolaceBeforeDawn
  • 958
  • 1
  • 8
  • 16
  • If you want to use jQuery in your code on jsFiddle site, you need to choose it from options available in dropdown on left section "Frameworks & Extensions" - see corrected jsFiddle in my answer below – gregjer Feb 28 '13 at 20:13

5 Answers5

4

Change selector from '.mybox' to '.myBox'

$(".myBox").click(function(){
 window.location=$(this).find("a").attr("href"); 
 return false;
});

Updated your jsFiddle (corrected typo and added jQuery which was missing in your example) -> http://jsfiddle.net/ryRnU/17/

gregjer
  • 2,823
  • 2
  • 19
  • 18
2

The class on your <div> element is myBox (with an uppercase B), but your selector is:

 $('.mybox')

with a lowercase B. Selectors are case-sensitive so it needs to be:

$('.myBox').click(function() {
    window.location=$(this).find("a").attr("href");
    return false;
});
Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
1

The other answers are correct, but in addition to the other answers, you also didn't set a jQuery API to use with your fiddle.

Also, I like to place the jQuery code inside a $(document).ready(function(){...}) function, to make sure the code is loaded as soon as the document is done loading.

This code should work on any website, with a jquery script imported to it. http://jsfiddle.net/ryRnU/8/

Gabi Barrientos
  • 1,746
  • 3
  • 23
  • 37
1

Try this.

$(document).ready(function(){
     $(".myBox").bind('click', function(){
        console.log($(this));
         window.location=$(this).find("a").attr("href"); 
         return false;
    });
});
Hasan Erzi
  • 11
  • 3
0

Your selector is wrong, Use it like this :

$(".myBox").click(function(){
 window.location=$(this).find("a").attr("href"); 
 return false;
});
Mandeep Pasbola
  • 2,631
  • 2
  • 26
  • 50