I have been trying to figure this out for a few hours, and it isn't working. Basically, on the frontend of a node with horizontal tabs, I need to have additional links that can switch the display of the tabs to the specific tab. My guess was to use the fragment url (#some_fieldset_id), but that does not work. It only switches to that tab if the page is loaded with that fragment, not when the link href is the fragment.

The other solution I have tried has been to trigger a click on the tab element, but no luck there either. The short code I tried unsuccessfully was this:

$('.horizontal-tab-button-1').trigger('click');

Does anybody have a quick and easy solution?

Much appreciated!

Comments

nils.destoop’s picture

Status: Active » Closed (fixed)

.horizontal-tab-button-1 is the list item. You need to trigger the click event from the link inside that list item.

$('.horizontal-tab-button-1 a').trigger('click'); should do the trick

geraldito’s picture

Thanks, that works.

MO-2’s picture

Category: feature » support
Status: Closed (fixed) » Active

How did you implement the above code? What would I need to do to create links to specific tabs? I'm a noob to php and would like to place a link on the node view page to each tab.

nils.destoop’s picture

Version: 7.x-2.x-dev » 7.x-1.x-dev
Status: Active » Fixed

The above code is javascript code. So if you add it to you themes javascript, this should work.

You have following link:
<a href="#" class="open-tab-1">open</a>

Then you have following js in your javascript file:
$('a.open-tab-1).bind('click', function() { $('.horizontal-tab-button-1 a').trigger('click');});

MO-2’s picture

@zuuperman

Thanks for the tip - I think I'm close.

I created this link (going for 3rd tab over - i.e., tab-2)

<a href="#tab2" class="open-tab-2">open</a>

I updated my theme's js file a few different ways. Here are my attempts:

Attempt #1


(function ($) {
  Drupal.My_Theme = Drupal.My_Theme || {};
  
  Drupal.My_Theme.initContactForm = function(){
    $('#edit-submitted-contact-name').placeholder({value:'Name*'});
    $('#edit-submitted-contact-email').placeholder({value: 'Email*'});
    $('#edit-submitted-contact-subject').placeholder({value: 'Subject*'});
    $('#edit-submitted-contact-phone').placeholder({value: 'Phone*'});
    $('#edit-submitted-contact-message').placeholder({value: 'Message'});
  }
  
  Drupal.behaviors.actionMy_Theme = {
    attach: function (context) {
      $('.btn-backtotop').smoothScroll({
        speed: 350
      });
      $('.view-rows-2').each(function(){
        $(this).find('.grid-inner').matchHeights();
      });
      $('#comments .comment').filter(':last').addClass('last');
      Drupal.My_Theme.initContactForm();
    }
  };
  

// Attempt #1 Snippet:  

(function ($) {
$('a.open-tab-2').bind('click', function() { $('.horizontal-tab-button-2 a').trigger('click');});
})
  
  
  
})(jQuery);

Attempt #2


(function ($) {
  Drupal.My_Theme = Drupal.My_Theme || {};
  
// Attempt #2 Snippet: 

  $('a.open-tab-2').bind('click', function() { $('.horizontal-tab-button-2 a').trigger('click');});

  
  Drupal.My_Theme.initContactForm = function(){
    $('#edit-submitted-contact-name').placeholder({value:'Name*'});
    $('#edit-submitted-contact-email').placeholder({value: 'Email*'});
    $('#edit-submitted-contact-subject').placeholder({value: 'Subject*'});
    $('#edit-submitted-contact-phone').placeholder({value: 'Phone*'});
    $('#edit-submitted-contact-message').placeholder({value: 'Message'});
  }
  
  Drupal.behaviors.actionMy_Theme = {
    attach: function (context) {
      $('.btn-backtotop').smoothScroll({
        speed: 350
      });
      $('.view-rows-2').each(function(){
        $(this).find('.grid-inner').matchHeights();
      });
      $('#comments .comment').filter(':last').addClass('last');
      Drupal.My_Theme.initContactForm();
    }
  };
  
  
})(jQuery);

Also, can you confirm that this is a different technique to the one on this thread?

https://drupal.org/node/1917674#comment-7839693

The use case I'm trying to solve is where you have links at the top of the page to each tab and that clicking the links will:

1 - open the tab
2 - jump to the tab anchor on the page
3 - not reload the page

Anymore hints or help? Thanks ahead of time!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Dmytro Litvinchuk’s picture

Issue summary: View changes

Thanks a lot! It also to work for Drupal 8. I created a module that adds bookmark link for Field Group.
Module here

dadderley’s picture

The code in #4 works really well in Horizontal Tabs.

One thing though, if you use
<a href="#" class="open-tab-1">open</a>
it will open the tab but go to the top of the page.

This works much nicer
<a href="javascript:void(0);" class="open-tab-1">open</a>