Problem with style references in existing javascript code

Having problems with DHTML Menu? There is usually somebody here who knows the answer.
Post Reply
Gayle
Beginner
Beginner
Posts: 4
Joined: Mon Jun 14, 2004 6:01 am

Problem with style references in existing javascript code

Post by Gayle »

I have been trialling your DHTML Menu and so far it has everything I'm looking for and more and is working brilliantly. Unfortunately however I've just discovered that one of my other scripts is now no longer working. The script is..

function expand(el) {
el2=eval(el)
if(el2.style.display == "none"){
el2.style.display="";
el2.expanded=true;
}
else {
el2.style.display="none";
el2.expanded=false;
}
}

and it expands or hides a div layer which is defined for example as...

<div id="alertbodyPaid style="display:none">

..a table with info etc usually here...

</div>

and is called via an href ..

<a href="##" class="green" target="_self" onClick="expand('alertbodyPaid'); return false" >Click Here to view</a>

I am now getting the following error message when clicking on the Href..

"Object doesn't support this property or message"

This is something I use quite frequently throughout the site and I need them to be able to work together. Although I'm able to write some basic javascript functions this is beyond my level of knowledge. It is something that seems to happen when the DHTML Menu is actually drawn. If I remove the drawmenu command there is no problem. I'm thinking that perhaps the default style properties have been changes in some way. Can you please help ...

Thanks
Gayle
Gayle
Beginner
Beginner
Posts: 4
Joined: Mon Jun 14, 2004 6:01 am

Post by Gayle »

Solved the problem. Don't know why but if i change the variable el2 to elval it no longer brings back an error. Phew. If anyone could tell me why however I'd be quite interested.

Thanks
Gayle
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

Hi Gayle,

el2 is used internally by the menu system to refer to a menu item (e.g., element 2). Your function sets el2=eval(el). Since el2 isn't declared within the function, it won't be treated as a local variable (i.e., local to that function) if a global variable of the same name already exists. Since an el2 already existed globally, your function was assigning to the global el2, instead of a variable within your function. I'm betting that the conflict also screwed up part of the menu.

Anyway, changing the variable name in the function removed the conflict. You could probably also have resolved the conflict by declaring el2 locally, inside of the function. I.e., instead of el2=eval(el) you could have put var el2=eval(el). This would make a local el2 for use inside the function, but cut off access to the global el2 from within the function (which wouldn't have been a problem anyway).

So... you asked ;)

Kevin
Gayle
Beginner
Beginner
Posts: 4
Joined: Mon Jun 14, 2004 6:01 am

Post by Gayle »

I did ask and thanks very much for your reply. :oops: A useful lesson to remember.

Cheers
Gayle [/img]
Post Reply