<DIV> tag question

First time I’ve messed with this tag. The example I am using is straight from a book.

This example produces the error “WoodMenuDiv is not defined” When you mouseover the image.

It looks like it is failing in the mouseover event. Never makes it to the function.

Thanks.

Simple code -

<script language=JavaScript>
function showMenu(src)
{
alert(“test”);
}
</script>
<html>
<body>

<div id=“WoodMenuDiv” Class=“DivMenu” onmouseout=“return hideMenu(this)”>
<table border=0 cellspacing=1 cellpadding=1 id=“WoodTable”>
<tr>
<TD id=“WoodOak” RollOver RollOut
onclick=“goPage(‘parcelquery.htm’)”
Class=“TDMenu”>
Oak Timber
</TD>
</tr>
</table>
</div>
<IMG ID=“WoodMenuImage” SRC=“identify_1.gif” Style=“position:absolute;left:10;top:75”
onmouseover=“return showMenu(WoodMenuDiv)”
onmouseout=“return hideMenu(WoodMenuDiv)”>
</body>
</html>

I’m a little rusty with my JavaScript, but try using single quotes around WoodMenuDiv in the onmouseover and onmouseout events:

onmouseover=“return showMenu(‘WoodMenuDiv’)”
onmouseout=“return hideMenu(‘WoodMenuDiv’)”

Thanks Joe,

hmmmm. Not sure if that did it. I have some other things to finish up and will do more testing on Monday.

The “showMenu” function seems to be expecting an object (such a reference to a div tag) to be passed in as a parameter (note the use of “this” as the parameter in the other mouse events. “this” is an object variable pointing to the element that raised the event). The error is occurring because there’s no object defined with the name “WoodMenuDiv”.

In order for this to work you need to be able to get hold of the object that represents the div tag. You can do this like this:



onmouseover="return showMenu(document.all('WoodMenuDiv'))"
onmouseout="return hideMenu(document.all('WoodMenuDiv'))"


Now, the problem here is that “document.all” is IE specific and is not a ratified standard (even though it is a sort of de-facto one by virtue of working on ninety-something percent of browsers). It won’t work on the Mozilla derived browsers or Opera(?).

If you need the cross-browser compatability it’s fairly easy to build in, but I don’t want to complicate matters if you’re just trying stuff out. If you want me to go into the compatability stuff then post a follow up and I’ll sort out some functions for you.

Instead of document.all, I have used:


document.getElementById('WoodMenuDiv')

This seems to work in newer browsers, anyway. I don’t know what you mean by building in cross-browser compatibility, but if you’re suggesting code that looks something like this:


if (ns) {
  ...
} else if (ie) {
  ...
}

then I’m not speaking to you. :wink:

Where is the “hideMenu” function in your code? And what is the expected result?

You’re also not showing the “goPage” function…can you show a little bit more code?

Hi all,

Thanks for all your responses.

I eliminated the goPage and hidemenu code just to make things easier to sort through. Those functions aren’t being called when the error occurs.

Snetho, I can and will post more of the code on Monday. It’s nothing fancy.

Armilla, you are right. It’s looking for an object, but not finding it. But should’nt <div id=“WoodMenuDiv”… define the object?

I’ll be back Monday, have a nice weekend.

The div tag creates the object, but you can’t reference it by its id alone. This is true of any tag-generated object, not just divs. You have to use a method to refer to it. If you like, in your code, you can say something like:


WoodMenuDiv = document.getElementById('WoodMenuDiv')

and then you should be able to do refer to the object simply as WoodMenuDiv.

Y’know, I would’ve sworn document.getElementById didn’t work in IE. But there it is, right there in the documentation and featuring prominently in a load of code I wrote just a few months ago. Some days you just ain’t firing on all cylinders.

Just ignore me and listen to Achernar.

AFAIK, both “id” and “class” are CSS. I think you are looking for "name, as in:



<div name="blah"...>


Thanks again everyone. I’ll try this out on Monday.

It burns my britches when I follow step by step instructions in a JavaScript help book, but the code doesn’t work.

Well I’m not sure what you’re trying to achieve, but hopefully this will help.

On function “toggleText” you’ll see two methods for identifying which object you’re referring, they are “window[oDiv]” and “document.getElementById(oDiv)”…they both work in IE, but I cannot speak for any other environment…good luck.



<script language="JavaScript">

  function toggleText(oDiv) {
    var sMsg = (window[oDiv].innerText == "Old message") ? "New message" : "Old message";

    document.getElementById(oDiv).innerText = sMsg;
  }

  function expandMenu(oDiv) {
    var submenu = window[oDiv].style;

    submenu.display = (submenu.display=="none") ? "" : "none";
  }
	
</script>
<button onClick="expandMenu('WoodMenuDiv');">Show / Hide</button>

<div>
   <span id="WoodMenuDiv">Old message</span>
</div>

<button onClick="toggleText('WoodMenuDiv');">Toggle</button>



Snetho - That works in IE but not NS. Thanks though.

Still having the same problem.

WoodMenuDiv = document.getElementById(‘WoodMenuDiv’); did not help.

I also tried onmouseover=“return showMenu(‘WoodMenuDiv’)”

The alert pops ‘undefined’.

Here is all the code. -

<script language=JavaScript>
function showMenu(src)
{
alert(src.type);
}
function hideMenu(menuToHide)
{
menuToHide.style.left = -200;
menuToHide.style.top = -1000;
}
function goPage(src)
{
window.location.href = src;
}

WoodMenuDiv = document.getElementById(‘WoodMenuDiv’);
</script>
<html>
<body>
<div id=“WoodMenuDiv” Class=“DivMenu” onmouseout=“return hideMenu(this)”>
<table border=0 cellspacing=1 cellpadding=1 id=“WoodTable”>
<tr>
<TD id=“WoodOak” RollOver RollOut
onclick=“goPage(‘parcelquery.htm’)”
Class=“TDMenu”>
Oak Timber
</TD>
</tr>
</table>
</div>

<IMG ID=“WoodMenuImage” SRC=“identify_1.gif” Style=“position:absolute;left:10;top:75”
onmouseover=“return showMenu(WoodMenuDiv)”
onmouseout=“return hideMenu(WoodMenuDiv)”>
</body>
</html>