0

I'm trying to use jquery's trigger function to simulate a click on a personal google Australian search page that I'm developing. From what I've read you can use trigger to just simulate a click on a link but I seem to be having problems and google seems to be very stingy with its info. Here's the code:

<a class="link" href="<?php if(isset($newurl)) { echo $newurl; }?>" target="_blank">tab link</a>

<script type="text/javascript">
  $("a .link").trigger('click');
</script>     

Thanks very much! Sam

3 Answers 3

3
  $("a.link").trigger('click'); ( without space)
Sign up to request clarification or add additional context in comments.

Comments

1

You have a space between a and .link; remove it and you should be OK. (a.link means find the <a> tag with class link, while a .link means find elements with class link inside <a> tags).

Comments

0

The trigger method will fire the click event, but the click event will not cause the default behaviour of the link. It will only cause any associated click event handlers to be executed.

I'm not sure if you have a click event handler bound to the link that you haven't shown, but I'm just assuming what you're trying to do is follow the link.

If that is what you're trying to do, you can bind an event handler to the link and redirect the user in there. For example:

$("a.link").click(function() {
    window.location = "somewhere.html";
});

3 Comments

How do I go about adding event handlers to the link?
@killkrazy - I've updated my answer. You can use the jQuery click method to bind a click event handler to an element.
Good suggestion but I'm trying to get the href to pop up in a tab in google chrome and I thought I could get round the default behaviour of google. Google chrome doesn't allow window.open("localhost/123", '_blank'); unless it's in a 'actual click event'.

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.