Need help fixing a JavaScript

B

Brenton

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
 
R

RobG

Brenton said:
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';


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top