|  | Show/Hide ... Need help in simple funcion |  | |
| | | shapper |  |
| Posted: Sun Jun 08, 2008 9:44 pm Post subject: Show/Hide ... Need help in simple funcion |  |
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 |
| |
| | | RobG |  |
| Posted: Tue Jun 10, 2008 12:35 am Post subject: Re: Show/Hide ... Need help in simple funcion |  |
| |  | |
On Jun 9, 7:44 am, shapper <mdmo...@gmail.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++]) [/quote] 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'; [/quote] 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]} }
|
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 |
| |
|
|