473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Optimizing a tab switcher script

13
Hi all,

I am a beginner with Javascript and i am trying to make a simple script to switch tabs with use of classes because im much better in styles.

My javascript is:

Expand|Select|Wrap|Line Numbers
  1. ]function switchClassName(obj,fromClass,toClass){
  2.     var stringClass = obj.className;
  3.     if (stringClass.match(fromClass)) {
  4.         stringClass = stringClass.replace(fromClass,toClass);
  5.         obj.className = stringClass;
  6.     }
  7.     else {
  8.         stringClass = stringClass.replace(toClass,fromClass);
  9.         obj.className = stringClass;
  10.     }
  11. }
  12.  
  13. function tabSwitcher(obj) {
  14.     var tabCount = document.getElementById('tabSet').getElementsByTagName('a');
  15.     var contentCount = document.getElementById('contentSet').getElementsByTagName('div');
  16.     for (var i = 0; i < tabCount.length; i++) {
  17.         (tabCount[i].className = '' )
  18.     }
  19.     switchClassName(obj,'current','');
  20.     for (var j = 0; j < tabCount.length; j++) {
  21.         if ((tabCount[j].className) == 'current') {
  22.             currentCount = j
  23.         }
  24.     }
  25.     for (k = 0; k < contentCount.length; k++) {
  26.         contentCount[k].className = 'off'
  27.     }
  28.     contentCount[currentCount].className = 'on'
  29. }
My CSS is:

Expand|Select|Wrap|Line Numbers
  1. .on { display: block; }
  2. .off { display: none; }
  3. .current { background-color: silver; }
and my HTML is:

Expand|Select|Wrap|Line Numbers
  1. <ul id="tabSet">
  2.     <li><a href="#" onclick="tabSwitcher(this);">tab 1</a></li>
  3.     <li><a href="#" onclick="tabSwitcher(this);">tab 2</a></li>
  4.     <li><a href="#" onclick="tabSwitcher(this);">tab 3</a></li>
  5. </ul>
  6. <div id="contentSet">
  7.     <div class="on">Content 1</div>
  8.     <div class="off">Content 2</div>
  9.     <div class="off">Content 3</div>
  10. </div>
It does work but it feels like the script can be much smaller. Can someone help with it please?

Thank you

Greetings,

Paul
Sep 12 '07 #1
2 1405
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

Two possible modifications:
  1. Instead of switching classes, you could just set the style.display property to "none" or "block", e.g.
    Expand|Select|Wrap|Line Numbers
    1. obj.style.display="block";
  2. Instead of two for loops, have one for loop and check for the tab className. If it equals "current", then set the content to "on", otherwise to "off"
Sep 13 '07 #2
PaulVS
13
Thank you for your reply i will work on it :)
Sep 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Ian Richardson | last post by:
I'm writing a large Javascript application (uncompressed source around 400K) which is doing almost all the initialisation it needs to in a just-in-time manner. However, I have included an option...
2
by: Christo | last post by:
Hi all! (N00b meat here-- :) Anyway, the header of this message tells you what I am seeking. The audience for my site is the lowest level tech user (because my agency is a social service...
5
by: johnny | last post by:
hi all, I would like to know if the more and more websites offering the 3 font sizes option by style switching are just following a trend or have chose the best solution by an...
1
by: Kerberos | last post by:
I found an interesting "menu", I don't know how you call it. It displays a link for 2 seconds, then it displays another link for 2 seconds, and so on. Here it is: http://www.alibaba.com under...
1
by: Amanda H. | last post by:
I posted about this a few days ago, and I got a few suggestions that fixed part of my problem. I am using a PHP script to switch external style sheets when the viewer clicks on the style link. ...
9
by: Jason | last post by:
I have translated my website into Japanese, so if someone visits this page in English: http://dheera.net/jason/about/interests/music/index.html I want them to be able to click on word...
0
by: Marcel Brekelmans | last post by:
Hello, I'm making an application, but sofar I can't get its icon to appear in the task switcher. I have created an iconfile with three icons in it: 16x16, 32x32, 48x48, 24bpp. I have assigned it...
2
by: Gary Brown | last post by:
Hi, How do you remove an icon from the task switcher menu? (The one you get with ALT-TAB.) I think I asked this before but never got an answer. Now I have another app that shows an icon for...
3
by: Dave Angel | last post by:
I have implemented the 'A list apart' styleswitcher which works great. I now have: - A permanent stylesheet - A normal contrast stylesheet - A high contrast stylesheet The switcher simply...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.