Copy an href Value to the Container Using JQuery

Sometimes you may need to make a whole div clickable, but instead of wrapping an <a> tag around it all you can get JQuery to do it for you by retrieving the link contained within it somewhere. This is an advantage if you want just one line of text to be wrapped in the <a> for SEO reasons, or if you are working with a CMS you don't want your client playing with links and putting them in the right place.

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

Note: For IE you need to reference the link to the root of the domain. Here I have done this by prepending "/" at the begining.

Tags: , , , ,

About Keiron

Web Developer based in the UK. Click here if you want to work with me