Word Cracker의 잡다한 정보 모음

How to make a whole ‘div’ clickable using jQuery

0 1

How can it be possible to make a whole ‘div’ clickable?

<div class="clickable" url="http://google.com">

In this case, you can use the following jQuery script to redirect users to the url specified when clicking the ‘div’ area: 

window.location = $(this).attr("url");
return false;
// Source: http://stackoverflow.com

Here, return false is used to avoid an event bubbling. Some recommend to use event.preventDefault() rather than return false. Please refer to  here for more about event.preventDefault() and return false.

If you want to specify the url directly within the jQuery script, please use the following:

window.location = 'http://www.google.com';

Related Posts