0

I'm new to programming and have a question related to HTML and Javascript. I have a HTML page with 100 links on it. I'm looking for a way wherein if I click on the first link then the rest of the links are clicked after it without me having to click them manually. Is there a way to do it?

Please help!!!

2
  • Pop-ups and pop-unders. What else. Boo!!! Commented May 14, 2011 at 2:29
  • Thank you so much for answering my question. This is what I used: <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("a:first").click(function() { $("a:not(:first)").click(); }); }); </script> </head> <body> <a href="google.com">google</a>; <a href="yahoo.com">yahoo</a>; </body> </html> But when I click on google, then the other page does not open. Where am I going wrong? Commented May 14, 2011 at 2:49

3 Answers 3

2
$("a:first").click(function() {
    $("a:not(:first)").click();
});

Sample working code: http://jsfiddle.net/EJY8s/

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for answering my question. This is what I used: <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("a:first").click(function() { $("a:not(:first)").click(); }); }); </script> </head> <body> <a href="google.com">google</a> <a href="yahoo.com">yahoo</a> </body> </html> But when I click on google, then the other page does not open. Where am I going wrong?
The problem with this answer is it relies on target="_blank" being set on the <a> elements, which your demo code doesn't have. My answer is more foolproof as it doesn't require this.
2
$('a').each(function() {
    $(this).click();
});

For full-syntax head to Jquery site documentation.

Comments

2

The following bit of Javascript will make it so you can click any one of those 100 links and open them each in a new window. 100 impressions, how nice.

$a = $("a");
$a.click(function(e){
    e.preventDefault();
    $a.each(function(){ window.open(this.href); });
});

Demo: jsfiddle.net/JB2YF

If you only want the first <a> to open all links, it's an easy tweak:

$("a:first").click(function(e){
    e.preventDefault();
    $("a").each(function(){ window.open(this.href); });
});

2 Comments

Thank you so much for answering my question. This is what I used: <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("a:first").click(function() { $("a:not(:first)").click(); }); }); </script> </head> <body> <a href="google.com">google</a>; <a href="yahoo.com">yahoo</a>; </body> </html> But when I click on google, then the other page does not open. Where am I going wrong?
From a quick glance, using that answer won't work because it will try and open all the links in the same current window which isn't going to work properly. Just use the second part of my answer above instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.