23

I'm figuring out why this simple script is not working:

jQuery.noConflict();
jQuery(document).ready(function() {
    jQuery('.next_button a').trigger('click');
});

noConflict is necessary because I also load prototype/scriptaculous in this page.

If I replace .trigger('click') with another function (es: .css(...) this works well. Only triggering seems to go broken.

4
  • Your question is incomplete without a test case that demonstrates the problem. See jsFiddle Commented May 3, 2011 at 9:10
  • I can't post the website link, sorry, only in private, in case Commented May 3, 2011 at 9:17
  • What Gary Green answered is probably what you are looking for, but without the rest of the code its impossible to know. Commented May 3, 2011 at 9:19
  • I understand but I cant post more, thanks anyway Commented May 3, 2011 at 9:47

4 Answers 4

68

How can I simulate an anchor click via jquery? Check this link and see this answer by Stevanicus.

$('a#swaswararedirectlink')[0].click();

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

10 Comments

Can you tell why the [0] is necessary?
[0] gets the first (DOM) element. The .click() function call is calling the browser's built-in 'click' function (not jquery's)
this should be the accepted answer since it provides a solution, i.e., [0].
Excellent! Worked fine without errors. I put as $('a.sliderLinks').trigger('click'); . But not worked. Then I put your code. superb! thanks again.
damn! why is the [0] necessary? weird that click() doesn't get trigger the other selector, what does "[0] gets the first (DOM) element" do? (Tahir reply)
|
50

You can only trigger a click that jQuery has created. It's one of jQuery's cute little quirks.

4 Comments

Good advice, i.e. use the trigger method AFTER you've established the click listener
Not only that, Pete but this also means default anchor tag use
+1 YOU! This saved me from breaking my desk and throwing out my computer! A note to people who may be working with backbone + requirejs. If your trigger events on UI elements are not working, you may just have a handle on the wrong jquery object wherever you are calling it.
Trigger after Listener. Trigger AFTER listener. Hours wasted until I found this out. +1 @PeteHerbertPenito
12

as what Gary has answered. the code below will not work.

$('#border_wrap').trigger('click');    
$('#border_wrap').click(function(e){
    console.log("clicked1");
});

but this will.. :)

$('#border_wrap').click(function(e){
    console.log("clicked1");
});
$('#border_wrap').trigger('click'); 

1 Comment

Thanks @keithics, I was doing blunder mistake which you have explained above.
0

I thought that this demo would not work but it does (Chrome 12). It will alert two messages, one for each click event. One is created by jQuery and one is native but I thought that only jQuery events could be triggered.

Edit: Yes the click does not follow href.

Edit 2: So the event you want to manually trigger is actually an event created by a Prototype carousel plugin. For the code below I am assuming it is this one. If that is the case, why can't you just use fire the event using Prototype or natively… like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style type="text/css">
        #carousel-wrapper {
            width:100px;
            height:100px;
            overflow:hidden;
            border:1px dashed red;
        }
        #carousel-content {
            width:500px;
        }
        #carousel-content .slide {
            float:left;
            width:100px;
            height:100px;
        }
    </style>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.3/scriptaculous.js"></script>
    <script type="text/javascript" src="http://prototype-carousel.googlecode.com/files/carousel-min.js"></script>
    <script type="text/javascript" src="https://github.com/kangax/protolicious/raw/5b56fdafcd7d7662c9d648534225039b2e78e371/event.simulate.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
        jQuery.noConflict();

        function fakeClick(event, anchorObj) {
          if (anchorObj.click) {
            anchorObj.click()
          } else if(document.createEvent) {
            if(event.target !== anchorObj) {
              var evt = document.createEvent("MouseEvents");
              evt.initMouseEvent("click", true, true, window,
                  0, 0, 0, 0, 0, false, false, false, false, 0, null);
              var allowDefault = anchorObj.dispatchEvent(evt);
              // you can check allowDefault for false to see if
              // any handler called evt.preventDefault().
              // Firefox will *not* redirect to anchorObj.href
              // for you. However every other browser will.
            }
          }
        }
    </script>
</head>
<body>
<div id="carousel-wrapper">
    <div id="carousel-content">
        <div class="slide">1</div>
        <div class="slide">2</div>
        <div class="slide">3</div>
    </div>
</div>
<div>
    <a href="javascript:" class="carousel-jumper" rel="slide-1">Jump to slide 1</a>
    <a href="javascript:" class="carousel-control" rel="prev">Previous slide</a>
    <a href="javascript:" class="carousel-control" id="next" rel="next">Next slide</a>
</div>
<script type="text/javascript">
    new Carousel('carousel-wrapper', $$('#carousel-content .slide'), $$('a.carousel-control', 'a.carousel-jumper'));

    document.observe("dom:loaded", function() {
//        $$('a[rel="next"]')[0].simulate('click');
        fakeClick(event, document.getElementById('next'));
    });
</script>
</body>
</html>

There are two examples in this demo of event firing (one is commented out but you can switch for the same result). One is from Trigger an event with Prototype which uses event.simulate.js and one using the fakeClick() function from How can I simulate a click to an anchor tag?. Either of which is working for me in Chrome 12.

4 Comments

Thanks to all of you but still doesn't work even if I tried your various solutions..I don't really know. If could help..the <a> element i'm trying to autoclick is the next button of a scriptaculous carousel..that maybe has autoclicking disabled by design ?? mmmmm..
Which carousel are you using?
It wasn't made by me, it's called from carousel.js and it's named as 'HP carousel"...
Why do you need to use jQuery if it's a Prototype carousel? Updated my answer with a potential solution, assuming you are using prototype-carousel.

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.