April 10, 2010

Open All Hyperlinks at Once using jQuery – the right way




If you have a bunch of hyperlinks on the page and you try opening them by using $(“a”).click(), they won’t open. This is because you need to first define a click event on that element.

Here’s a solution (shared originally by Owen) that works:

<html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
title>Open all Hyperlinks at Once</title>
<
script type="text/javascript"
src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js">
</
script>
<
script type="text/javascript">
$(function() {
$("#open").click(function() {
$("a").click();
});

$("a").click(function() {
window.open($(this).attr('href'));
});
});
</script>
</
head>
<
body>
<
a href="http://www.dotnetcurry.com">DotNetCurry</a>
<
a href="http://www.devcurry.com">DevCurry</a>
<
a href="http://www.sqlservercurry.com">SQLServerCurry</a>
<
input id="open" type="button" value="Open All" /><br />
</
body>
</
html>

See a Live Demo

You may also want to look at Convert all Links to Open in a New Page



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Open All Hyperlinks at Once using jQuery – the right way"
 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions