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(theidP refix, theidNum)
{
// show / hide clicked menu element
if (document.getEl ementById)
{
var switch_id = document.getEle mentById(theidP refixtheidNum);
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.getEle mentById(theidP refix + theidNum);
if(menu_statust heidPrefixtheid Num != '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.class Name = 'show';
You should keep variables local by using the 'var' keyword unless you
really want them to be global:
var switch_id.class Name = 'show';
menu_statusthei dPrefixtheidNum = 'show';
menu_status[theidPrefix + theidNum] = 'show';
}
else
{
switch_id.class Name = 'hide';
menu_statusthei dPrefixtheidNum = 'hide';
menu_status[theidPrefix + theidNum] = 'hide';
// hide non - clicked menu elements
n = 1;
var n = 1;
while( document.getEle mentById(theidP refixn) )
while( document.getEle mentById(theidP refix + 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.getEle mentById(theidP refixn);
var hide_id = document.getEle mentById(theidP refix + n);
hide_id.classNa me = 'hide';
menu_statusthei dPrefixn = '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(theidP refix, theidNum)
{
if (!document.getE lementById) return;
var el = document.getEle mentById(theidP refix + theidNum);
if ('object' == typeof menu_status && menu_status != el){
menu_status.cla ssName = '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