By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

Show/hide multiple elements with one link using javascript

P: n/a
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 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
<it******@gmail.com> wrote in message news:11**********************@g44g2000cwa.googlegr oups.com...
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[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.
Oct 15 '05 #2

P: n/a
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 '05 #3

P: n/a
i<it******@gmail.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.com...
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[i] )
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
Oct 15 '05 #4

P: n/a
<a href="#" class="navlink" onclick="obj_toggle('titlelinks',
'show_hide_nav_controller', 'Show Nav', 'Hide Nav');"><span
id="show_hide_nav_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

Oct 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.