1

I have a tab on my page which contains tabs like this:

<ul>
  <li class="3 tabs__item active uiTabItem" role="presentation" data-aura-rendered-by="79:126;a" data-aura-class="uiTabItem" data-original-width="183">
    <a class="tabHeader" aria-selected="true" data-tab-name="3" tabindex="0" role="tab" aria-controls="73:126;a" title="Files &amp; Contacts" href="?tabset-563b2=3" data-aura-rendered-by="80:126;a" data-special-link="true">
      <span data-aura-rendered-by="81:126;a">
     </span>
      <span class="title" data-aura-rendered-by="83:126;a">Files &amp; Contacts</span>
    </a>
  </li>
</ul>

Within the code I'd like to get the tab label in String format. So for the code above I'd like to return "Files &amp; Contacts", I'd like to do this using just vanilla JS as opposed to using jQuery or any other framework.

This is the code I have so far, I can target the correct active tab but not sure how to just retrieve the text:

var selectedTab = document.querySelector('.tabs__item.active');
console.log(selectedTab);

CodePen link

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
InvalidSyntax
  • 9,131
  • 20
  • 80
  • 127

1 Answers1

0

You could use innerText or textContent :

var selectedTab = document.querySelector('.tabs__item.active');
console.log(selectedTab.innerText);
//OR
console.log(selectedTab.textContent);
//OR
console.log(selectedTab.innerText.replace(/&/g, '&amp;'));
<ul>
  <li class="3 tabs__item active uiTabItem" role="presentation" data-aura-rendered-by="79:126;a" data-aura-class="uiTabItem" data-original-width="183">
    <a class="tabHeader" aria-selected="true" data-tab-name="3" tabindex="0" role="tab" aria-controls="73:126;a" title="Files &amp; Contacts" href="?tabset-563b2=3" data-aura-rendered-by="80:126;a" data-special-link="true">
      <span data-aura-rendered-by="81:126;a"><!--render facet: 82:126;a--></span>
      <span class="title" data-aura-rendered-by="83:126;a">Files &amp; Contacts</span>
    </a>
  </li>
</ul>
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101