Click to See Complete Forum and Search --> : Help! Problems with calling HTML elements from script


justin001
10-10-2003, 05:19 AM
Please help!

The following functions build DIVs and handle their events respectively. As you will see, the DIV builder has the mouseover et al references coded into it. It passes an ID, that has been assigned to the DIV or inner DIV element to the menuEventHandler function, which then uses document.all[passed id] to get the event out of the body. But it doesn't work. When I look at the object that it calls, it has two properties – both with the name of ID, and both are objects. Why is it not calling the HTML element with the id that is passed? Thanks in advance.

I ONLY NEED IE6+ COMPLIANCE, AND I HAVE CODED FOR THIS BROWSER SPECIFICALLY

Script Scrap:
function createMenus(pArray,pParent)
{
var content="";
var tempChild;
var menuID="menu"+menuElAr["menus"].length;
var triggerID="";
content+="<div class='menuContainer' id='"+menuID+"' "+
"onmouseover=\"menuEventHandler('"+menuID+"','over')\" onmouseout=\"menuEventHandler('"+menuID+"','out')\""+
"><table border='1' cellspacing='0' cellpadding='0'>";
menuElAr["menus"][menuElAr["menus"].length]=new MenuRefObj(menuID,pParent||"");
for (var i=0;i<pArray.length;i++)
{
triggerID="trigger"+menuElAr["triggers"].length;
if (pArray[i].child) tempChild=createMenus(pArray[i].child,triggerID);
content+="<tr><td class='menuTrigger' id='"+triggerID+"' "+
"onmouseover=\"menuEventHandler('"+triggerID+"','over')\" onmouseout=\"menuEventHandler('"+triggerID+"','out')\">"
if (pArray[i].ref) content+="<a href='"+pArray[i].ref+"'>";
content+=pArray[i].name;
if (pArray[i].ref) content+="</a>";
content+="</td></tr>";
menuElAr["triggers"][menuElAr["triggers"].length]=new TriggerRefObj(triggerID,pArray[i].ref||"",tempChild||"");
tempChild="";
}
content+="</table></div>\n";
HTMLMenuContent[HTMLMenuContent.length]=content;
return (menuID)
}

function menuEventHandler(pID,pAct)
{
var content="";
for (var i in document.all[pID])
content+=i+" "+document.all[pID][i][0]+"\n";
alert(content);

switch (pAct)
{
case "over":

if (findMenuEl(document.all[pID],"triggers"))
var obj=findMenuEl(document.all[pID],"triggers");
if (obj.mChild);
showHide(document.all[obj.mChild.mID],1);
break;
case "out":
break;
}
}

nauman73
10-10-2003, 06:04 AM
Hi

My understanding of your problem is that you are unable to access the object represented by the 'pID' passed to your 'menuEventHandler' function. That is, 'document.all[passed id]' (or 'document.all[pID]') is not returning the object required. Correct me if I am wrong.

I have analyzed your code and also tried your 'menuEventHandler' function in a test page of my own. Of course the page that I created was not created using your 'createMenus' function becasue your code is dependent on some other functions like 'findMenuEl' and data memebrs like 'menuElAr'. I also trimmed down your 'menuEventHandler' function. The page that I created is given below.


<html>
<head>
<script language="JavaScript">
<!--
function menuEventHandler(pID,pAct)
{
var obj = document.all[pID];
}
//-->
</script>
</head>
<body>
<div class='menuContainer' id='test' onmouseover="menuEventHandler('test','over')" onmouseout="menuEventHandler('test','out')">
<table border='1' cellspacing='0' cellpadding='0'>
<tr>
<td class='menuTrigger' id='trig' onmouseover="menuEventHandler('trig','over')" onmouseout="menuEventHandler('trig','out')">
<a href='http://cnn.com'>Test</a>
</td></tr>
</table>
</div>
</body>
</html>


Well, the variable 'obj' inside the 'menuEventHandler' in the above code does get a valid object for the pID passed to it. I have tried it on IE6.x. So I am not sure why your code is not catching it. May be if you provide some missing parts of your code, I can analyze it better.

Nauman

justin001
10-10-2003, 07:01 AM
Hi! Thanks for the answer. The problem I have is that I am passing a string to the event handling function. This string is the ID of the HTML element I need to refer to in the page. But when I use that string in conjunction with document.all[], I get a new object which doesn't have any HTML element properties, and I never made myself. I can't post the whole code, unfortunately... code is long.

Thanks,
Justin

nauman73
10-10-2003, 07:13 AM
Hi

You can attach your code as a zip file.

Nauman

justin001
10-10-2003, 07:22 AM
here it is