On May 17, 2012 In Coding with , tags

Make html elements clickable with javascript

This simple one line of JavaScript code will help us. make html elements clickable, which is great thing for touch screen phones. where as in touch screen phones other than iPhone and android, little unfriendly to touching headings, some might need clicking twice like my Samsung jet 2.

window.location = 'URL';

Above line will tell the browsers window location i.e, our desired url or a web page. if i wanted to make a div clickable and take the user to mobifreaks.com the code be as following.

<div onclick="window.location = 'URL'">
<!-- Some data -->
</div>

Making clickable elements in wordpress

The procedure is same, but we can’t add url manually, so lets automate them inside loop with the_permalink function

<div onclick="window.location = '<?php the_permalink(); ?>'">
<!-- Some data -->
</div>

The same follows to any HTML element

Note

Make sure to add some hyperlinks, Inside those clickable elements. So that we don’t block search engine bots, because of few lines of JavaScript