Show/Hide ... Need help in simple funcion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    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

    #2
    Re: Show/Hide ... Need help in simple funcion

    On Jun 9, 7:44 am, shapper <mdmo...@gmail. comwrote:
    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';[/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]
          }
        }
    [/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

    Comment

    Working...