/    Sign up×
Community /Pin to ProfileBookmark

Hiya,

I have different tabs that are all in the markup as default with only the first one having an exposed class. The others have hidden classes (display: hidden).

Each tab is invoked by an onclick even on the hyperlink, running this script:

[code]
function displayTab(carID, tabName) {
document.getElementById(carID + ‘_mnui_desc’).className = ‘tab’;
document.getElementById(carID + ‘_mnui_specs’).className = ‘tab’;
document.getElementById(carID + ‘_mnui_photos’).className = ‘tab’;
document.getElementById(carID + ‘_mnui_videos’).className = ‘tab’;
document.getElementById(carID + ‘_mnui_price’).className = ‘tab’;
document.getElementById(carID + ‘_mnui_build’).className = ‘tab’;
document.getElementById(carID + ‘_mnui_reviews’).className = ‘tab’;
document.getElementById(carID + ‘_mnui_scores’).className = ‘tab’;
document.getElementById(carID + ‘_mnui_events’).className = ‘tab’;
document.getElementById(carID + ‘_desc’).className = ‘hidden’;
document.getElementById(carID + ‘_specs’).className = ‘hidden’;
document.getElementById(carID + ‘_photos’).className = ‘hidden’;
document.getElementById(carID + ‘_videos’).className = ‘hidden’;
document.getElementById(carID + ‘_price’).className = ‘hidden’;
document.getElementById(carID + ‘_build’).className = ‘hidden’;
document.getElementById(carID + ‘_reviews’).className = ‘hidden’;
document.getElementById(carID + ‘_scores’).className = ‘hidden’;
document.getElementById(carID + ‘_events’).className = ‘hidden’;
document.getElementById(carID + ‘_mnui_’ + tabName).className = ‘active_tab’;
document.getElementById(carID + ‘_’ + tabName).className = ‘exposed’;
}
[/code]

My question is can it be made more efficient, i.e. without having to manually specify each tab?

[code]
<ul>
<li id=”2_mnui_desc” class=”active_tab”><a href=”#desc” onclick=”displayTab(‘2’, ‘desc’);return false;”>Description</a></li>
<li id=”2_mnui_specs” class=”tab”><a href=”#specs” onclick=”displayTab(‘2’, ‘specs’);return false;”>Specifications</a></li>
<li id=”2_mnui_photos” class=”tab”><a href=”#photos” onclick=”displayTab(‘2’, ‘photos’);return false;”>Photos</a></li>
<li id=”2_mnui_videos” class=”tab”><a href=”#videos” onclick=”displayTab(‘2’, ‘videos’);return false;”>Videos</a></li>
<li id=”2_mnui_price” class=”tab”><a href=”#price” onclick=”displayTab(‘2’, ‘price’);return false;”>Price List</a></li>
<li id=”2_mnui_build” class=”tab”><a href=”#build” onclick=”displayTab(‘2’, ‘build’);return false;”>Build</a></li>
<li id=”2_mnui_reviews” class=”tab”><a href=”#reviews” onclick=”displayTab(‘2’, ‘reviews’);return false;”>Reviews</a></li>
<li id=”2_mnui_scores” class=”tab”><a href=”#scores” onclick=”displayTab(‘2’, ‘scores’);return false;”>Scores</a></li>
<li id=”2_mnui_events” class=”tab”><a href=”#events” onclick=”displayTab(‘2’, ‘events’);return false;”>Events</a></li>
</ul>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@007JulienDec 30.2011 — Using an array for the values and [I]getElementsByTagName[/I], it's always possible to make loops to test the ids. It is only a shortcut of writing.

[CODE]
var vls='desc,specs,photos,videos,price,build,reviews,scores,events'.split(,);

function displayTab(carID, tabName) {var i,eli=getElementsByTagName('li'),ell=eli.length;
for (i=0;i<ell;i++) {
if (new RegExp(carID+'_mnui_'+eli[i],'g').test(eli[i].id)) eli[i].className = 'tab';
if (new RegExp(carID+'_'+eli[i],'g').test(eli[i].id)) eli[i].className = 'hidden';}
document.getElementById(carID + '_mnui_' + tabName).className = 'active_tab';
document.getElementById(carID + '_' + tabName).className = 'exposed';
}
[/CODE]

Besides [I]id[/I] and [I]name[/I] tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
Copy linkTweet thisAlerts:
@Declan1991Jan 04.2012 — You should put the event handler on the ul, not every li ([url=http://icant.co.uk/sandbox/eventdelegation/]event delegation[/url]). Then, figure out which tab should be displayed from the href of the link. Way easier to maintain (almost no duplication), and automatically works without JavaScript because all the ids match up by definition.

Also, you only need to keep track of the active tab, the others are automatically identifiable. So:&lt;ul id="tab2"&gt;
&lt;li class="active_tab"&gt;&lt;a href="#whatever"&gt;Whatever&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#elsewhere"&gt;Otherwise&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#faraway"&gt;No class&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

Accessed in CSS by:#tab2 li {
/* Not active */
}
#tab2 li.active_tab {
/* Active */
}

And a very brief outline in JavaScript:window.onload = function () { // usual warnings apply
var ul = document.getElementById("tab2"), lis = ul.getElementsByTagName("li");
ul.onclick = function (e) {
e = e || window.event; // Take care of IE
var t = e.target || e.srcElement;
if (t.nodeName === "A") { // mightn't work in &lt; IE 6, should be checked what exactly
hideAllTabs(); // iterate through all tabs and hide them
clearActive(); // iterate through lis and remove active class
t.parentNode.className = "active_tab"; // make this one active
showTab(t.href.substring(1)); // show the relevent tab, could also use title instead of href to have no #
}
}
};
If you have multiple sets of tabs, you can generalise that, and either have the JavaScript figure out what goes with what through id names (i.e. <ul class="tab nav"> matches the tabs listed in <div class="tab nav"> and <ul class="tab contents"> matches <div class="tab contents"> or some such scheme), or find the elements yourself onload.

Dream: it's possible to use :target to select the tab you want to show and need pretty much no JavaScript, but < IE 8 doesn't support that unfortunately!
×

Success!

Help @ldoodle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...