Re: toggle several elements

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Anthony Jones" <AnthonyJones@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:72BB224D-9E5D-4714-94E1-823B07E0C796@xxxxxxxxxxxxxxxx

"BD" wrote:

Hi all.
I currently use a js toggle function that works well for toggleing the
visibility of images div's &etc. with the only parameter being the
element
to toggle. Called using somthing like
onclick="toggle('div1') + toggle('div2')"
Now I need to make some changes because the next series of pages will
have
multiple elements (all divs) to be toggled. I need to have it toggle the
current element to hidden and the linked element to visible.
I seem to recall something like "toggle(document.getelementbyid.this)".
Is
that correct? If not could someone set me straight or even show a better
function please.

Try this:-

function toggle(displayDivID)
{
this.style.display = "none";
document.getElementById(displayDivID).style.display = "block"
}

call like this:-

onclick="toggle.call(this,'nextDivID')"

An alternative would be to track the ID of the currently displayed div in
a
variable so you can continue to call toggle as you have been. However
that
requires that an initial call to toggle be made onload with the ID of the
first div.

var msCurrentDivID = null;
function toggle(displayDivID)
{
if (msCurrentDivID )
document.getElementByID(msCurrentDivID).style.display = "none"

msCurrentDivID = displayDivID

if (msCurrentDivID )
document.getElementByID(msCurrentDivID).style.display = "block"
}


--
Anthony Jones - MVP ASP/ASP.NET

Hi Anthony.
Just to bring you up to date here is how I handled this.
Since I already had a function to toggle the div by id, and since the divs
in question all had the same class, I just added a function that hides all
of the divs of that class and then calls the toggle function to display the
div I want. It seems to work well and does what I needed.
Thanks for your input and I saved your example for future reference.

George


.



Relevant Pages

  • Re: toggle text in different locations on the same page
    ... have only really dabbled in javascript. ... I have a working text toggle in place on a web page. ... Not here as the inherited default style for a DIV ... the link is cancelled by returning false from the onclick handler (or ...
    (comp.lang.javascript)
  • Re: show hide divs - show last state upon reload/post
    ... The toggle works fine, although when I click on submit, I ... If the div was ... When you post a form in a window in a frameset, only that window is refreshed, not the window holding the frameset definition. ...
    (comp.lang.javascript)
  • Re: toggle several elements
    ... I currently use a js toggle function that works well for toggleing the ... first div. ... if (msCurrentDivID) ... Anthony Jones - MVP ASP/ASP.NET ...
    (microsoft.public.scripting.jscript)
  • Re: Problem with javascript in web page.
    ... I have this javascript function in the head ... The first line in the div is a bookmark. ... This will toggle fine but it does not jump to the very top of the page ...
    (microsoft.public.scripting.jscript)
  • Re: Problem with javascript in web page.
    ... You can safely delete this first clause. ... The first line in the div is a bookmark. ... This will toggle fine but it does not jump to the very top of the page ...
    (microsoft.public.scripting.jscript)