I'm starting a website using jQuery.
I'm trying to get a clean code with a main skeleton page - index.html - and call jQuery to load other components. So in the future if I can call my components the same way.
<head>
<title>my site</title>
</head>
<body>
<div id="sidebar">
<script>
$(function() {
$("#sidebar").load("components/sidebar.html");
});
</script>
</div>
<!-- Main Content -->
<div id="content">
</div>
</body>
</html>
And my sidebar.html is
<!-- Sidebar -->
<ul>
<!-- Nav Item - Dashboard -->
<li class="nav-item active">
<a class="nav-link" href="index.html"> element 1 </a>
</li>
<li class="nav-item active">
<a class="nav-link" href="index.html"> element 2 </a>
</li>
</ul>
<!-- End of Sidebar -->
It's working for now, but I want to display only an element using user data for example with this component.html is not working
<!-- Sidebar -->
<ul>
<!-- Nav Item - Dashboard -->
<li class="nav-item active">
<a class="nav-link" href="index.html"> element 1 </a>
</li>
<li class="nav-item active">
<a class="nav-link" href="index.html"> element 2 </a>
</li>
<script>
var access = 0;
if (access == 0) {
$(function() {
$(this).html(
'<li><a>element 3</a></li>')
});
</script>
</ul>
<!-- End of Sidebar -->
Maybe it's because the load("components/sidebar.html")
doesn't trigger my script in my sidebar.html file?
Is there a better way to do it?
Thanks a lot.