I have a problem with this script. At first I had the <div>
s hard-coded and it was working fine. But then I needed to alter the navigation bar dynamically because I changed the (number of) links.
The problem is that the id's are not registering when they are injected with element
.innerHTML = (html string).
How can I fix this?
Here is the code:
javascript:
function constructNav() {
var submenu_layer_innerHTML = "";
var top_nav_innerHTML = "<UL class=\"nav_category\">";
for (i=0; i<links.length; i++) {
submenu_layer_innerHTML = "<div class=\"submenu\" id=\"submenu" + i + "\" onMouseOut=\"timeHideNav('" + i + "','1000')\"> </div>";
top_nav_innerHTML += "<LI id=\"link" + i + "\"><a href=# onMouseOver=\"expandNav('" + i + "');\">" + links[i] + "</a></LI>";
}
top_nav_innerHTML += "</UL>";
document.getElementById("top_nav").innerHTML = top_nav_innerHTML;
document.getElementById("submenu_layer").innerHTML = submenu_layer_innerHTML;
}
html:
<script type="text/javascript" src="scripts.js"> </script>
<script type="text/javascript">
var links =["link 1",
"link 2",
"link 3",
"link 4",
"link 5",
"link 6"];
</script>
<body onLoad="constructNav();">
<div id="submenu_layer"> </div>
<div id="top_nav"> </div>
I'm getting this error in firefox:
line 35: document.getElementById("submenu"+pageNo) is NULL
document.getElementById("submenu"+pageNo) is NULL
that started when I changed <div id="submenu1"></div>
(10 of those) to 6 of those dynamically generated in javascript - Ozzy 2012-04-03 22:15
You are overwriting submenu_layer_innerHTML
during each iteration of the for
loop. Make sure to use +=
.
try jQuery live function ..
$("div.something").live('click', function(){
// do something...
});
constructNav is not defined Try this instead: http://jsfiddle.net/5GjNs/
constructNav()
is defined. I changed the order though, so that links
is before the scripts.js
import, so that links
is defined before the scripts are run - Ozzy 2012-04-03 22:14