Show/hide multiple elements with one link using javascript

Discussion in 'Javascript' started by itunes66@gmail.com, Oct 14, 2005.

  1. Guest

    how can i do this i already have a function to show/hide elements with
    one link but how can i show/hide multiple elements

    here is the script

    function obj_ref(object)
    {
    if (document.getElementById)
    {
    return document.getElementById(object);
    }
    else if (document.all)
    {
    return eval('document.all.' + object);
    }
    else
    {
    return false;
    }
    }

    function obj_toggle(object, open_close, open_text, close_text)
    {
    var object = obj_ref(object);
    var icone = obj_ref(open_close);

    if( !object.style )
    {
    return false;
    }

    if( object.style.display == 'none' )
    {
    object.style.display = '';
    icone.innerHTML = close_text;
    }
    else
    {
    object.style.display = 'none';
    icone.innerHTML = open_text;
    }
    }
     
    , Oct 14, 2005
    #1
    1. Advertising

  2. <> wrote in message news:...
    > how can i do this i already have a function to show/hide elements with
    > one link but how can i show/hide multiple elements
    >


    A maintainable solution would be to create an array of objects, each element consisting
    of the parameter list for each function call:

    var paramTable=[{id : 'objId1', otherId : 'something', openText : 'xxx', closeText : 'yyy'}, {......}, ........ ];

    Then have your link call a function that loops through the array passing each list to obj_toggle( )
    in turn.

    for(var i=0; i < paramTable.length; i++)
    with( paramTable )
    obj_toggle( id, otherId, openText, closeText );


    A couple of points regarding your code:

    obj_ref() would be better returning null on failure rather than false, and its return value
    should be tested in obj_toggle() before checking for its .style property.

    --
    S.C.
     
    Stephen Chalmers, Oct 15, 2005
    #2
    1. Advertising

  3. Guest

    well i got that code off my forum and modified it to change txt and not
    an icon but can you give me an example of you code like a full code
    example like i posted
     
    , Oct 15, 2005
    #3
  4. i<> wrote in message news:...
    > well i got that code off my forum and modified it to change txt and not
    > an icon but can you give me an example of you code like a full code
    > example like i posted
    >


    I don't know your parameter values so you must substitute them yourself
    in the table below.
    If you add more elements, maintain the exact syntax used; don't put a
    comma after the last element.
    A suitable HTML link is shown at the bottom.

    <script type='text/javascript'>

    function multiToggle()
    {
    var paramTable=[

    {id : 'objId1', openClose : 'dunno1', openText : 'xxx', closeText : 'yyy'},
    {id : 'objId2', openClose : 'dunno2', openText : 'xxx', closeText : 'yyy'},
    {id : 'objId3', openClose : 'dunno3', openText : 'xxx', closeText : 'yyy'}

    ];

    for(var i=0; i < paramTable.length; i++)
    with( paramTable )
    obj_toggle( id, openClose, openText, closeText );

    }

    // All your original code here

    </script>

    <A href='#' onclick='multiToggle();return false'>Toggle Visibility</A>


    --
    Stephen Chalmers http://makeashorterlink.com/?H3E82245A
     
    Stephen Chalmers, Oct 15, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    3
    Views:
    13,526
    Walter Wang [MSFT]
    Jul 27, 2006
  2. Replies:
    1
    Views:
    1,684
    Neredbojias
    Oct 17, 2007
  3. Biranchi Narayan Panda

    using ajax to show data in multiple labels one by one...

    Biranchi Narayan Panda, Feb 18, 2010, in forum: ASP .Net
    Replies:
    2
    Views:
    1,098
    Biranchi Narayan Panda
    Feb 20, 2010
  4. Eero Tuomenoksa

    Show & hide multiple div tags with one click

    Eero Tuomenoksa, May 28, 2006, in forum: Javascript
    Replies:
    2
    Views:
    364
    TheBagbournes
    May 28, 2006
  5. ll
    Replies:
    2
    Views:
    662
    Thomas 'PointedEars' Lahn
    May 1, 2008
Loading...

Share This Page