Need help fixing a JavaScript

Discussion in 'Javascript' started by Brenton, Aug 24, 2006.

  1. Brenton

    Brenton Guest

    Hi

    I'm new to the group so if I make a major blunder posting this here I
    apologise in advance.

    I'm not a Programmer by any stretch of the imagination and I've been
    trying to sort out a small JavaScript that I found here:
    http://www.interspire.com/content/a...-Expanding-DHTML-Menu-With-CSS-and-JavaScript

    if I use what is supplied it works fine, but a little further down
    there is a enhancement that I just can't get to work. It seems to be
    because of the { and }.

    This is the script that needs to be fixed:
    menu_status = new Array();

    function showHide(theidPrefix, theidNum)
    {

    // show / hide clicked menu element
    if (document.getElementById)
    {
    var switch_id = document.getElementById(theidPrefixtheidNum);

    if(menu_statustheidPrefixtheidNum != 'show')
    {
    switch_id.className = 'show';
    menu_statustheidPrefixtheidNum = 'show';
    }
    else
    {
    switch_id.className = 'hide';
    menu_statustheidPrefixtheidNum = 'hide';


    // hide non - clicked menu elements
    n = 1;
    while( document.getElementById(theidPrefixn) )
    if(n !== theidNum)
    var hide_id = document.getElementById(theidPrefixn);
    hide_id.className = 'hide';
    menu_statustheidPrefixn = 'hide';

    n;
    }
    }
    }

    If anybody here could give me a pointer I would greatly appreciate it.

    Regards
    Brenton
     
    Brenton, Aug 24, 2006
    #1
    1. Advertising

  2. Brenton

    RobG Guest

    Brenton wrote:
    > Hi
    >
    > I'm new to the group so if I make a major blunder posting this here I
    > apologise in advance.
    >
    > I'm not a Programmer by any stretch of the imagination and I've been
    > trying to sort out a small JavaScript that I found here:
    > http://www.interspire.com/content/a...-Expanding-DHTML-Menu-With-CSS-and-JavaScript
    >
    > if I use what is supplied it works fine, but a little further down
    > there is a enhancement that I just can't get to work. It seems to be
    > because of the { and }.
    >
    > This is the script that needs to be fixed:
    > menu_status = new Array();
    >
    > function showHide(theidPrefix, theidNum)
    > {
    >
    > // show / hide clicked menu element
    > if (document.getElementById)
    > {
    > var switch_id = document.getElementById(theidPrefixtheidNum);


    You can't just munge together identifiers and hope the JavaScript
    interpreter will work it out. Guessing that 'theidPrefix' and
    'theidNum' are strings and that you want to concatenate them:

    var switch_id = document.getElementById(theidPrefix + theidNum);


    > if(menu_statustheidPrefixtheidNum != 'show')


    Here you treat the array 'menu_status' as if it was a plain object, so
    why not declare it as an object:

    var menu_status = {};

    Then use it as one:

    if ('show' != menu_status[theidPrefix + theidNum])


    > {
    > switch_id.className = 'show';


    You should keep variables local by using the 'var' keyword unless you
    really want them to be global:

    var switch_id.className = 'show';


    > menu_statustheidPrefixtheidNum = 'show';


    menu_status[theidPrefix + theidNum] = 'show';


    > }
    > else
    > {
    > switch_id.className = 'hide';
    > menu_statustheidPrefixtheidNum = 'hide';


    menu_status[theidPrefix + theidNum] = 'hide';


    > // hide non - clicked menu elements
    > n = 1;


    var n = 1;


    > while( document.getElementById(theidPrefixn) )


    while( document.getElementById(theidPrefix + n) )


    > if(n !== theidNum)


    if(n != theidNum)

    I think you mean all the following statements to be inside the 'if' block:

    {

    > var hide_id = document.getElementById(theidPrefixn);


    var hide_id = document.getElementById(theidPrefix + n);


    > hide_id.className = 'hide';
    > menu_statustheidPrefixn = 'hide';


    menu_status[theidPrefix + n] = 'hide';


    > n;


    I think you mean to increment n here:

    n++;


    > }
    > }
    > }


    No guarantees...

    I seems rather pointless storing the display property value in an object
    since you can get it directly from the element.

    Guessing that you want to show only one at a time, just store a
    reference to the one that is currently visible. Then when you want to
    show another one, hide the one that the variable references, show the
    one you want and change the reference to the new one, e.g.:

    var menu_status;

    function showHide(theidPrefix, theidNum)
    {
    if (!document.getElementById) return;
    var el = document.getElementById(theidPrefix + theidNum);
    if ('object' == typeof menu_status && menu_status != el){
    menu_status.className = 'hide';
    }
    el.className = 'show';
    menu_status = el;
    }

    That probably won't suit, but may give you an idea of how to go about it.


    --
    Rob
     
    RobG, Aug 24, 2006
    #2
    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. JSOUL
    Replies:
    3
    Views:
    4,120
    Jonathan N. Little
    Feb 11, 2006
  2. darrel
    Replies:
    3
    Views:
    3,583
    Darrel
    Jun 2, 2006
  3. Erland

    Error, Please help fixing it !!!

    Erland, Jul 3, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    314
    roberto
    Jul 3, 2006
  4. Domino
    Replies:
    5
    Views:
    417
    dorayme
    Nov 5, 2006
  5. shaun
    Replies:
    10
    Views:
    674
    Dennis Lee Bieber
    Sep 6, 2012
Loading...

Share This Page