Show/hide multiple elements with one link using javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • itunes66@gmail.com

    #1

    Show/hide multiple elements with one link using javascript

    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.getEl ementById)
    {
    return document.getEle mentById(object );
    }
    else if (document.all)
    {
    return eval('document. all.' + object);
    }
    else
    {
    return false;
    }
    }

    function obj_toggle(obje ct, open_close, open_text, close_text)
    {
    var object = obj_ref(object) ;
    var icone = obj_ref(open_cl ose);

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

    if( object.style.di splay == 'none' )
    {
    object.style.di splay = '';
    icone.innerHTML = close_text;
    }
    else
    {
    object.style.di splay = 'none';
    icone.innerHTML = open_text;
    }
    }

  • Stephen Chalmers

    #2
    Re: Show/hide multiple elements with one link using javascript

    <itunes66@gmail .com> wrote in message news:1129316009 .940978.129890@ g44g2000cwa.goo glegroups.com.. .[color=blue]
    > 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
    >[/color]

    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.leng th; i++)
    with( paramTable[i] )
    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.


    Comment

    • itunes66@gmail.com

      #3
      Re: Show/hide multiple elements with one link using javascript

      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

      Comment

      • Stephen Chalmers

        #4
        Re: Show/hide multiple elements with one link using javascript

        i<itunes66@gmai l.com> wrote in message news:1129340885 .339435.220730@ o13g2000cwo.goo glegroups.com.. .[color=blue]
        > 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
        >[/color]

        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.leng th; i++)
        with( paramTable[i] )
        obj_toggle( id, openClose, openText, closeText );

        }

        // All your original code here

        </script>

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


        --
        Stephen Chalmers http://makeashorterlink.com/?H3E82245A


        Comment

        • Jordan Pittman

          #5
          Re: Show/hide multiple elements with one link using javascript

          <a href="#" class="navlink" onclick="obj_to ggle('titlelink s',
          'show_hide_nav_ controller', 'Show Nav', 'Hide Nav');"><span
          id="show_hide_n av_controller"> Hide Nav</span></a>

          is the code i used to trigger the link to show and hide the navigation
          inside a p element and also i need to be able to define the objects in
          the function multi toggle and i need it to have infinite number of
          things it can toggle kinda like an array

          e.g.
          say i had 2 elements with the same id i need them both to be toggled
          basically it is obj_toggle except it can toggle multiple elements (i am
          using this for the <tr> element) at one time

          Comment

          Working...