JavaScript UI Libraries & Blazor Components Suite – Smart UI › Forums › Tabs › Change tab title
Tagged: angular tab, Angular Tabs, custom element, custom element tabs, new tab button custom label, react tabs, smart-tabs, tab, Tabs, tabs with custom label, web components, web components tabs
- This topic has 3 replies, 3 voices, and was last updated 5 years ago by admin.
-
AuthorPosts
-
October 24, 2019 at 3:48 am #100447adminKeymaster
How do I change the titles of existing tabs from javascript, without resorting to direct manipulation of the DOM by targeting the div that contains the title? Changing the “label” property on a <smart-tab-item> after it has been added to a <smart-tabs> does not appear to change the displayed tab title.
October 24, 2019 at 5:41 am #100448adminKeymasterHello Starlight Sparkle,
Thank you for your interest. The proper way to update a tab item’s label and/or content is by calling the method update. Here is an example of setting only the title:
document.getElementById('tabs').update(0, 'Updated title');
If you wish to change the content, too, pass a third argument, e.g.:
document.getElementById('tabs').update(0, 'Updated title', 'Updated content');
Here is also a demo that showcases this method among others: https://www.htmlelements.com/demos/tabs/insert-remove-update/.
Best regards,
Dimitar
Smart HTML Elements Team
https://www.htmlelements.comOctober 26, 2019 at 8:47 am #100458adminKeymasterDamn, can’t believe I missed that in the documentation. Sorry and thanks!
But on a related note, is it possible to set the default label that will be used by new tabs created through the built-in “new tab” button?October 28, 2019 at 8:22 am #100463adminKeymasterHi Starlight Sparkle,
the newly added item via the “addNewTab” button always has a label of “New Tab” and it’s not possible to change that via API. However since the new item becomes the selected item by default you can accomplish the same result by binding to thechange
event and callingupdate
method on it. Here’s how to do it:document.querySelector('smart-tabs').addEventListener('change', function (event) { const newTabIndex = event.detail.index; //Get all tab items inside the element const allTabItems = this.querySelectorAll('smart-tab-item'); //Update the label if (allTabItems[newTabIndex].label === 'New Tab') { this.update(newTabIndex, 'New Custom Label'); } });
Best Regards,
Christopher
Smart HTML Elements Team
https://www.htmlelements.com -
AuthorPosts
- You must be logged in to reply to this topic.