0

I'm trying to trigger a click event on page load but it doesn't seem to be working..

I can't see any errors in the Firebug console...

Jquery:

jQuery(document).ready(function($) {
    $(window).load(function(){
      alert("Loaded.");
      $('.a-category .active').trigger('click'); 
    });
});

I added the alert just to check if it's working. And it does alert..

Any help will be appreciated!

6
  • 2
    take out the $(window).load(function(){ part. don't think it will solve your problem, but it's not needed unless you really want to wait for all elements on the page to load (images and whatnot) Commented Jun 18, 2013 at 18:26
  • show the html. You probably dont need the space between .a-category .active Commented Jun 18, 2013 at 18:27
  • did u try using just $(el").click()? Commented Jun 18, 2013 at 18:28
  • @sgroves I did take it out but it doesn't do anything either. Commented Jun 18, 2013 at 18:30
  • @karthikr <div class="a-category active">Websites</div> Commented Jun 18, 2013 at 18:30

2 Answers 2

1

You probably want:

$('.a-category.active').trigger('click');

Since the html is

<div class="a-category active">Websites</div>

The listener should be for both the classes present on the same div

Currently, you are looking for class active in the children/grandchildren nodes of node with class a-category, hence the 'click' is not getting triggered.

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

2 Comments

Thanks for this. It's still not working. Would it be ok if I put the whole hierarchy so for example $('#mainDiv.categories.a-category.active')...?
It does not matter - It should have worked. Did you remove $(window).load(function(){ like @sgroves recommended
0

This code is correct

$(document).ready(function() {
    alert("Loaded.");
    $('.a-category .active').trigger('click'); 
});

if that doesn't work, your selector (the '.a-categry .active') might be incorrect. are you trying to select an element with both classes, or one inside the other?

you can also just call $('.a-category').click(); on an element, which will trigger the click on it

4 Comments

"this code is correct" then you go on to say why it might not be. it should be very easy for you to test which one works and update your answer with the correct solution
no, you misunderstood. The code that I posted is correct jQuery and will work in any situation as long as the element the selector is looking actually exists on the page. Then i stated that the selector he's using may not be correct in his specific situation, which i suspect might be the problem
ah, I didn't see that. either way, my answer was correct and didn't deserve a downvote. if I had seen his posted html, I would not have just suggested to change the selector, but would have told OP to use $('.a-category.active').click();
i didn't downvote you (but it looks like who did removed it, so no worries)

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.