0
<a class="example" href="https://example.com/index.php">hellau</a>
<a class="example" href="https://example1.com/index.php">hellau1</a>
<a class="example" href="https://example2.com/index.php">hellau2</a>

Need to get and go to second href somehow.

$('.example')[1].click();

doesn't work sticks to one random link until entry is removed

var link = $(".example");
link.click();
window.location.href = link[1].attr("href"); 

doesn't work either

Any way to make it click specific text part or something along those lines?

Text and links are dynamic

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yer
  • 39
  • 1
  • 7
  • 1
    To be clear, what do you want to accomplish? Click on first link causing redirection to second link `href` or Trigger a click on second link? – rrk Aug 14 '15 at 11:56
  • possible duplicate of [Jquery how to trigger click event on href element](http://stackoverflow.com/questions/7999806/jquery-how-to-trigger-click-event-on-href-element) – ebilgin Aug 14 '15 at 12:01
  • trigger a click on second link, in this example. But it could be any link max up to 50 random links – Yer Aug 14 '15 at 12:02

4 Answers4

1

Need to get and go to second href somehow.

You are trying to trigger the click event of second anchor element. However it will not trigger the default action of redirecting to href. You will need to get the href of second element and then set windows href location to it.

Like this:

window.location.href = $(".example:eq(1)").attr('href');
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

Using trigger()

$('.example').eq(1).trigger('click');

Alternative way

$('.example').eq(1).on('click');
rrk
  • 15,677
  • 4
  • 29
  • 45
0

use the below code

$("a.example").click(function(){

      var href = $(this).attr('href');
      window.location.href = href;

}
dom
  • 1,086
  • 11
  • 24
-1

Here is an example code for what you look : Jquery

$("a").click(function(){
    alert($(this).attr("href"));
});
Bhavin Solanki
  • 4,740
  • 3
  • 26
  • 46
Sagar
  • 259
  • 2
  • 3
  • 14