1

Having some problems with JavaScript / jQuery.

We're currently using a "shortcode" from Visual Composer (WordPress plugin) that creates (horizontal) tabs. We'we made some custom changes, but only visually (images/css).

We're trying to make it so that when you click on a tab you automatically scroll down to the content.

I've tried wrapping the script in

jQuery(document).ready( function({});
jQuery(window).ready( function({});

and also tried replacing .ready() with .load()

jQuery(".pws_tabs_controlls_item").children().click(function(){
    jQuery('html, body').animate({
         scrollTop: jQuery(".ts-fancy-tabs-container").offset().top
    }, 700, 'swing', function () {});
});

However! If you copy the entire script and paste it in the Console in Chrome/Firefox it will work as intended, based on that I'd say that the script it self works, it feels like it's not loading or registering or w/e.

Is there any (obvious) problem/mistake we've made?

I'd be happy to provide additional information if neccessary (e.g. the website address)

Any help would be greatly appreciated!

4
  • 1
    There is no ready event for window. Also, are the children of .pws_tabs_controlls_item in the DOM when it's loaded, or are they injected via JavaScript?
    – Andy
    Commented Oct 16, 2015 at 13:45
  • Ya, sounds like you are trying to handle some dynamic elements. In this case, delegate event
    – A. Wolff
    Commented Oct 16, 2015 at 13:46
  • 1
    Did you forget a closing parenthesis after the ready function or is it a just a typo in your post? Commented Oct 16, 2015 at 13:47
  • Yeah the tabs are loaded with javascript after page load, so yeah, probably dynamic elements.
    – Dym
    Commented Oct 16, 2015 at 14:07

1 Answer 1

3

The VisualComposer plugin renders only after the document is ready, and that's why it's not working.

The best way to solve your problem is by using jQuery's event delegation, e.g:

jQuery(document).on('click', '.pws_tabs_controlls_item > a', function() {
    jQuery('html, body').animate({
         scrollTop: jQuery(".ts-fancy-tabs-container").offset().top
    }, 700, 'swing', function () {});
});

UPDATE

After your comment, I can see that the anchor tags inside your .pws_tabs_controlls_item elements already have a click event listener, and it also uses event.stopPropagation(), so my code above will never be executed.

The other way to solve your problem, is by dirty-checking when the element is available, e.g:

function waitForAvailability(selector, callback) {
  if (jQuery('selector').length === 0) {
    callback();
  }
  else {
    setTimeout(function() {
      waitForAvailability(selector, callback);
    }, 0);
  }
}

And then, you can use the code you were already using, something like that:

waitForAvailability('.pws_tabs_controlls_item', function() {
  jQuery(".pws_tabs_controlls_item").children().click(function(){
    jQuery('html, body').animate({
      scrollTop: jQuery(".ts-fancy-tabs-container").offset().top
    }, 700, 'swing', function () {});
  });
});
10
  • Just wondering, isn't .pws_tabs_controlls_item static so we could delegate at its level? (even it doesn't really matter i guess)
    – A. Wolff
    Commented Oct 16, 2015 at 13:48
  • It would be quicker to delegate the event to .pws_tabs_controlls_item. No point bubbling all the way up to document if it is static
    – Andy
    Commented Oct 16, 2015 at 13:50
  • @A.Wolff I'm not sure, but it seems that it's created by VisualComposer dynamically. In fact, while we have lack of information, I tried to give an answer that will actually work for the OP.
    – Buzinas
    Commented Oct 16, 2015 at 13:51
  • @Andy yeah, I agree with you. In fact, firstly I typed jQuery('.pws_tabs_controlls_item').on('click', 'a', function() {, but I thought it could be wrong if those items are being created dynamically.
    – Buzinas
    Commented Oct 16, 2015 at 13:52
  • @Buzinas You are completly right of course regarding posting an universal working answer. I posted previous comment because i was thinking you know exactly this plugin, that's why i was wondering :)
    – A. Wolff
    Commented Oct 16, 2015 at 13:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.