I've got two javascripts on a page:
One is a script that gets some JSON from a server and then builds out some navigation markup and writes it to a div.
The other is a script that does something on click of one of the nav items.
So basically,
document.getElementById("writeNav").innerHTML += writeNav;
would replace:
<ul id="writeNav" class="navbar-nav mr-auto">
<li class="nav-item"> <a class="nav-link" href="#"><span>Home</span></a> </li>
</ul>
with:
<ul id="writeNav" class="navbar-nav mr-auto">
<li class="nav-item"> <a class="nav-link" href="#"><span>Home</span></a> </li>
<li class="nav-item "> <a id="undefined" class="nav-link" href="#"><span>link 1</span></a></li>
<li class="nav-item "> <a id="undefined" class="nav-link" href="#"><span>link 2</span></a></li>
</ul>
The Jquery, for the purpose of troubleshooting, would just launch an alert onClick of any of the ".nav-item" classes.
<script>
$(document).ready(function(){
$('.nav-link').click(function(){
alert('click function was called.');
});
});
</script>
If I comment out the "getElementById" line and click "home" it triggers the alert. If I don't comment it out, and click "home", it does not trigger the alert.
Why? What am I doing wrong?