469,286 Members | 2,442 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

Need help fixing a JavaScript

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/ar...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

Aug 24 '06 #1
1 1198
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/ar...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
Aug 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by George Hester | last post: by
9 posts views Thread by Dr John Stockton | last post: by
3 posts views Thread by MS News | last post: by
18 posts views Thread by chimalus | last post: by
12 posts views Thread by googlegroups | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.