#1 (permalink)  
Old 09-08-2008, 02:49 AM
Registered User
 
Join Date: Aug 2008
Posts: 5
Is there a better way to code this

I have a javascript code written as:
Code:
 function change_it(itemID){
      // Toggle visibility between none and inline
      if ((document.getElementById(itemID).style.display == 'none'))
      {
        document.getElementById(itemID).style.display = 'inline';
      } else {
        document.getElementById(itemID).style.display = 'none';
      }
  }
  
  function close_it(itemID){
    document.getElementById(itemID).style.display = 'none';
  }
And I have like say 5 menu items (menu1, menu2, menu3, menu4,menu5)

On click of a particular menu, i want the table displayed, relevant to that menu and hide the others. To do this I have called the javascript as follows:

example:
HTML Code:
<table id="menu_tbl" cellpadding=5 style="display:none;" size=10>
   <tr>
   <td><a href="#h" onClick="change_it('menu1') & close_it('menu2') & close_it('menu3') & close_it('menu4') & close_it('menu5')">menu1</a></td>
   <td><a href="#e" onClick="change_it('menu2') & close_it('menu1') & close_it('menu3') & close_it('menu4') & close_it('menu5')">menu2</td>
   </td>
               .....
As you see, more the menu's the longer gets the onClick line .. I am wondering if there is a better way of doing this

Thank you,
Evu
Reply With Quote

  #2 (permalink)  
Old 09-08-2008, 05:39 PM
Deadeye's Avatar
Moderator
 
Join Date: Aug 2005
Location: San Diego, CA
Posts: 274
Send a message via MSN to Deadeye
thats one way of doing it, or have a close all and call the before you "open" the correct menu.


Code:
function change_it(itemID) {
closeEverything();
document.getElementById(itemId).style.display = 'inline';
}
function closeEverything() {
document.getElementById("menu1").style.display = 'none';
document.getElementById("menu2").style.display = 'none';
document.getElementById("menu3").style.display = 'none';
...
}

OR

you can close everything and open the item all within the same method.

Code:
function change_it(itemID) {
switch(itemID)
case 1:
   document.getElementById("menu1").style.display = 'inline';
   document.getElementById("menu2").style.display = 'none';
   document.getElementById("menu3").style.display = 'none';
case 2:
   document.getElementById("menu1").style.display = 'none';
   document.getElementById("menu2").style.display = 'inline';
   document.getElementById("menu3").style.display = 'none';
....

}
I'd do it the first way that I've listed with the close everything of the 3 options listed so far.

Wesley
__________________
Don't forget to rate me A+++++++++++++++++!!!!!111one for the service you have received.
Reply With Quote
  #3 (permalink)  
Old 09-08-2008, 05:47 PM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,438
I agree with Deadeye. The best way to do this is to write a close_all function that will systematically loop through all of your items and hide them.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
  #4 (permalink)  
Old 09-20-2008, 04:30 PM
Registered User
 
Join Date: Aug 2008
Posts: 5
Sorry for the late reply. I was trying to use the first method to run the code. But somethings not working fine or it must be just that I am going wrong somewhere. Anyway with the help of your code I came up with something like this:
Code:
var a= new Array();
a[0]= "menu1";
a[1]= "menu2";
a[2]= "menu3";
a[3]= "menu4";

function change_it(itemID) {
document.getElementById(itemID).style.display='block';
var i= a.findindex(itemID);
b=a.slice(0,4);
b.splice(i,1);
document.getElementById(b).style.display='none';
}
It works fine for the first time but second time, when i click on the link it shows the portion of html and suddenly disappears. Could someone look at it and tell me where could the code be going wrong.

Thank you.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help with this code evuseem Programming and Scripting 4 08-26-2008 06:54 AM
PHP Code, Efficiency and Benchmarking curtiss Programming and Scripting 2 06-02-2008 06:42 PM
Google archives my site in HTML Code - why? Jim Mansfield Programming and Scripting 2 05-03-2007 08:17 AM
HTML Code: Centering Page kobedog84 Programming and Scripting 4 08-11-2006 01:35 PM
Please help. Web code displaying instead of result of code daisywheel Programming and Scripting 3 03-10-2006 07:30 AM


All times are GMT -5. The time now is 02:49 AM.

 
Bitrix
Clicky Web Analytics
CloudContacts
Maxtango


Subscribe to our feed | add to myYahoo!

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
© 1997-2007 HTMLCenter