Show/Hide ... Need help in simple funcion

S

shapper

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
 
R

RobG

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top