HighDots Forums  

Show/Hide ... Need help in simple funcion

Javascript JavaScript language (comp.lang.javascript)


Discuss Show/Hide ... Need help in simple funcion in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
shapper
 
Posts: n/a

Default Show/Hide ... Need help in simple funcion - 06-08-2008 , 05:44 PM






Hello,

I have the following function:
Code:
function show(id) { var allIds = ['Home', 'Biografia', 'Fotografias', 'EntreJaneiros', 'Trabalhos', 'Hiperligacoes'], ids, i=0; while(ids = allIds[i++]) { document.getElementById(ids).style.display=ids==id?'block':'none'; } }

I have 6 divs with Ids 'dHome', 'dBiografia', ...
And 6 anchors with Ids 'aHome', 'aBiografia', ...

So when I do show('Home') I want to:
1. Show dHome and hide all the other divs from the list
2. Hide aHome and show all the other anchors from the list

How to change my code to accomplish this?

Thanks,
Miguel

Reply With Quote
  #2  
Old   
RobG
 
Posts: n/a

Default Re: Show/Hide ... Need help in simple funcion - 06-09-2008 , 08:35 PM






On Jun 9, 7:44 am, shapper <mdmo... (AT) gmail (DOT) com> wrote:
Quote:
Hello,

I have the following function:
Code:
function show(id) { var allIds = ['Home', 'Biografia', 'Fotografias', 'EntreJaneiros', 'Trabalhos', 'Hiperligacoes'], ids, i=0; while(ids = allIds[i++])
Code:
I think it is better to use: var i = allIds.length; while (i--) { ids = allIds[i]; or var i = allIds.length; while (i) { ids = allIds(--i); or var i = allIds.length; do { ids = allIds(--i); ... } while (i); The last one shouldn't be used if the length of allIds has a chance of being zero.
Quote:
{ document.getElementById(ids).style.display=ids==id?'block':'none';
To answer your question below, try (wrapped for posting): document.getElementById('d' + ids).style.display = (('d' + ids) == id)? '' : 'none'; document.getElementById('a' + ids).style.display = (('a' + ids) == id)? '' : 'none'; When you want to show the element, don't set the display property value to 'block', set it to '' (empty string) so that the value can return to whatever it is by default or inheritance.
Quote:
} }
Quote:

I have 6 divs with Ids 'dHome', 'dBiografia', ...
And 6 anchors with Ids 'aHome', 'aBiografia', ...

So when I do show('Home') I want to:
1. Show dHome and hide all the other divs from the list
2. Hide aHome and show all the other anchors from the list
Alternatively you could use a class and modify the CSS rule instead
(this tends to be faster where there are many elements to change, but
in this case you won't notice much difference), or use a class
selector rather than id.


--
Rob


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.