6

I have a link that is on the page and I want to click it programmatically when the page loads.

Something like this:

$(document).ready(function () {

   // code to make the page think $('a.tagcloud-link') is clicked
});

The link is an AJAX call that displays the popular tags in Wordpress backend.

The function cannot easily be edited as it's dependent on many things.

So I want to trigger the AJAX link that calls the most popular tags on page load.

This will solve all my problems if it works.

That link is $('a.tagcloud-link')

Please, please help me on this I've spent hours pulling my hair out trying and failing to do this. Thank you.

4
  • 1
    did you try $('a.tagcloud-link').first().trigger('click'); Commented Oct 24, 2012 at 0:10
  • This didn't work @Tariqulazam Commented Oct 24, 2012 at 0:14
  • 1
    It appears from your comments that you are not outlining the actual problem you are having. Please elaborate on exactly what you are trying to do so the answers can be targeted to the specific use case. Commented Oct 24, 2012 at 0:18
  • @JamesSwift thanks for the tip, edited the question accordingly Commented Oct 24, 2012 at 0:30

11 Answers 11

8

Try this:

$('a.tagcloud-link')[0].click();

Using [0] gets a reference to the first DOM object in the jQuery object so this calls the DOM object's (non-jQuery) .click() method which will cause the navigation to the link's specified href.

Using jQuery's .click() method would call any click handler that you'd bound to the element, but won't cause the default behaviour to actually follow the link.

Really simple demo: http://jsfiddle.net/mtBav/

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

8 Comments

This didn't work. the function totally stopped working after trying to load that code on page load.
What I've shown would cause an error if no elements exist that match the 'a.tagcloud-link' selector, so make sure you've actually created the element first. The code in your fiddle does not create any such element, and you haven't shown any html, so... (Your fiddle isn't very helpful: you're supposed to post JS code into the JavaScript window (bottom left), not into the HTML window where you put it.)
The element exists. But I think something blocks it from being available to clicking outside the usual code because I've tried everything suggested so far. Sorry about the fiddle, fixed it.
But the code in your fiddle doesn't create the link element, so where does it exist? Is it in your HTML (which you don't show)?
Sorry, your fiddle isn't really functional since the PHP doesn't actually run, and for some reason it takes ages to load up so I can't really experiment with it. Here is a really simple demo that shows my code will work (placed in a document.ready handler): jsfiddle.net/mtBav
|
4

If that element owns an event handler which was bound with jQuery too, you might just call .trigger( 'click' ) or short .click();

$(function() {
    $('a.tagcloud-link').click();
});

Of course, at the time when you invoke .click(), that event handler must be bound already.

Comments

3
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("a#tobeclicked").click(function () {
// if it has to be href
var tohref = $(this).attr("href");
alert(tohref);
window.location=tohref;


// else you want add some functions

$("#mydiv").fadeIn();

});
$("a#tobeclicked").trigger('click');
});
</script>
<a id="tobeclicked" href="http://www.google.com">Go!</a>
<div id="mydiv" style="display:none;">Stroam</div>

1 Comment

hey man you said none of the answers worked for you check the above code which i have added. It works 100% man... I checked it 5 times and working perfectly!
1

If I correctly understood the question:

   $('a.tagcloud-link').click();

2 Comments

use that just after your link is inserted dynamicaly and appender to the DOM tree - #('body').append('<a class="tagcloud-link" href=""...>...</a>'); $('a.tagcloud-link').click();
The code is being added by normal HTML and not with .append() I'm trying to implement what you suggested using an inline <script> tag after the HTML code.
1

If you have already binded $('a.tagcloud-link').click(), it will call that function. The following code should work.

$(document).ready(function () {
 $('a.tagcloud-link').click();
});

But if you want to open the link in its href, you need to use window.location.

1 Comment

This didn't work. Obviously I'm doing something wrong as the link works when clicked manually and the code you've given should really work but it doesn't execute the action on page load :|
1

Try

$('a.tagcloud-link').click();

OR

$('a.tagcloud-link').trigger('click')

1 Comment

This didn't work. I should mention that the link that I want to click on when the page opens is itself a code generated by jQuery code dynamically. But I want to simulate a click on it when the document is loaded. surely there's a way?
1
$(function() {
     $('a.tagcloud-link').on('click', function() {
          // execute code here
     });
});

This is proper use of jQuery.

2 Comments

I want to simulate the link being clicked on page load. I don't think your code does that.
Then you should add more code, what do you want the script to do if the scripts 'thinks' that a.tagcloud-link is clicked?
1

To be honest with you, I think everyone else here is right and I think the question is framed wrong. But heres my attempt and I think all I did was just frame it a little different. Just trying to help.

document.addEventListener("DOMContentLoaded", function() {
    EventL();
});

// global
var tagLoad = false;

function eventL() {
    $('a.tagcloud-link').on('click', function() {
       tagLoad = true;
    });

    // you can repurpose this, its for page refresh right now. but
    // I think it helps with your general idea?
    $(window).load(function(){ 
       init();
    });
}

function init () {
    if (tagLoad == true) {
         // Load your tags.
    }
}

2 Comments

It might be framed wrong but you can tell me how to frame it, although seems like you didn't understand what I wanted to achieve. Anyway, thanks for trying to help!
I read somewhere that you want to simulate a click, are you looking for the visual appearance of that? or are you looking for something to remember that a button has been clicked on your wordpress template?
0

None of these helped. I fixed it myself by bypassing the link and executing it on load.

1 Comment

Yeah, I don't understand what these people don't get about click() not working lol. Same boat you are, trying to automate something in a Greasemonkey script.
0
function my_admin_head(){
echo 
'<script type="text/javascript">
    jQuery(function( $ ){ 
        $(\'a.tagcloud-link\').each( function(){
            tagBox.get( $(this).attr(\'id\') );
            $(this).unbind().click(function(){
                $(this).siblings(\'.the-tagcloud\').toggle();
                return false;
            });
        return false;
    });
});
</script>';
}
add_action('admin_head', 'my_admin_head');

Maybe it can help somebody.

Comments

0

Maybe the way you was using to access the element be mixing jquery syntax with javascript. I'v got this problem too, but I was trying to access a element by wrong way.

It was like this

$(document).ready(function(e){
        alert(location.hash);
        if (location.hash == "#22") {
            $("gallery div a")[1].click(); //without #
        };
    })

but should be like this

$(document).ready(function(e){
        alert(location.hash);
        if (location.hash == "#22") {
            $("#gallery div a")[1].click(); //with #, cause I was using an ID
        };
    })

And works :)

Comments

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.