1

How do I make an entire div as hyperlink with the same URL as <a> inside this div? My jQuery is probably wrong.

<div>
   <a href="some-url"></a>
</div>

var $href = $('div > a').attr('href');
$('div').on('click', function(event) {
        $(this).location.href=$href;
})

3 Answers3

2

Try invoke the child anchor's click while user clicking on the div,

$('div').on('click', function(event) { 
    $(this).children('a')[0].click(); 
});

DEMO

Note: we did not use $(this).children('a').click() because this wont invoke the natural click of that anchor tag.

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • @Rajaprabhu Aravindasamy Sorry for the previous question. Actually i got the answer by accident. At that time i didnt know the reason why $(this).click() dont work. From the comments only i got the reason. :) – Anoop Joshi P Jul 04 '14 at 11:26
  • @AnoopJoshi Yeah.. i am just implementing what i learnt over there.. :) – Rajaprabhu Aravindasamy Jul 04 '14 at 11:27
  • 1
    @RajaprabhuAravindasamy Yeah, I know ;). +1 for your special "Note" :) – Anoop Joshi P Jul 04 '14 at 11:29
  • @user3686925 Glad to help..! and by the way, if you think that anyone of the answers here has satisfied your need then try to accept it by clicking on the tick mark present inside it.. And it is not a compulsion.. :) – Rajaprabhu Aravindasamy Jul 04 '14 at 12:14
0

Use window.location to redirect the page

$('div').on('click', function(event) {

       $href =  $(this).find("a").attr('href');

        window.location.href= $href;
});
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
0

Use jquery:

$('div').click(function(event) {
       var href =  $(this).find("a").attr('href');
        window.location.href=href;
});

add css for pointer:

div{cursor:pointer}

UPDATED DEMO

Manwal
  • 23,450
  • 12
  • 63
  • 93