0

I am running following code on a link:-

$('#expertprofiles-category-filters .filterby > a').on("click", function () {
    // Figure out which category to show.
    var categoryId = $(this).attr('rel');

    // Hide the other subfilters.
    $("div.category-panel[rel!=" + categoryId + "]").slideUp(100);

    // Show the subfilters of the category.
    $("div.category-panel[rel=" + categoryId + "]").delay(100).slideDown(400);
});

This code runs fine but the problem is that I want to run this code on page load too, so that any one link is autaomatically selected.

I tried this:-

$("#expertprofiles-category-filters .filterby").find('a').trigger('click');

But it doesn't work ...

Thanks.

Tushar
  • 82,599
  • 19
  • 151
  • 169
Steve
  • 2,476
  • 7
  • 46
  • 90
  • are you sure that `$('#expertprofiles-category-filters .filterby > a')` and `$("#expertprofiles-category-filters .filterby").find('a')` targets the same elements? – lagerone May 11 '15 at 11:11
  • Yah, that's the intention. – Steve May 11 '15 at 11:16
  • 1
    Question which explained why .click() is not working with tag [link](http://stackoverflow.com/questions/1694595/can-i-call-jquery-click-to-follow-an-a-link-if-i-havent-bound-an-event-hand). – Tarick Fife May 11 '15 at 11:18

3 Answers3

1

Use the same selector

$('#expertprofiles-category-filters .filterby > a').trigger('click');
ozil
  • 6,357
  • 8
  • 29
  • 53
1

You may also do the following way,

$('#expertprofiles-category-filters .filterby > a').on("click", function () {
    // Figure out which category to show.
    var categoryId = $(this).attr('rel');

    // Hide the other subfilters.
    $("div.category-panel[rel!=" + categoryId + "]").slideUp(100);

    // Show the subfilters of the category.
    $("div.category-panel[rel=" + categoryId + "]").delay(100).slideDown(400);
}).trigger("click"); // triggers click on DOM ready
lshettyl
  • 8,016
  • 4
  • 23
  • 30
0

Try this

   $('#expertprofiles-category-filters .filterby > a').click();
Aman Uppal
  • 111
  • 1
  • 6