Firstly, I would like to say that I am new to javascript, jquery, JSON and most of my learning has been through the microsoft site, w3schools site, and Stack Overflow.
The issue at hand is I am pulling data from a web-api, but the data isn't showing in the web page. I know the data is there as it shows in the google inspect as well as when I step through the code in the editor.
The error being returned now is "blank2.html:241 Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'".
Below the code:
function menu(activemenu, roleid) {
var myul = document.createElement("ul");
myul.setAttribute("class", "nav nav-pills nav-sidebar flex-column");
myul.setAttribute("data-widget", "treeview");
myul.setAttribute("role", "menu");
myul.setAttribute("data-accordion", "false");
var uri = 'api/menu';
$.getJSON(uri)
.done(function (data) {
$('#sidemenu').empty();
$.each(data, function (key, item) {
var myli = document.createElement("li");
var n = String(item.roleid);
var x = n.search(String(roleid));
if (x > -1) {
if (activemenu == item.pagename) {
myli.setAttribute("class", "nav-item nav-link active");
} else {
myli.setAttribute("class", "nav-item nav-link");
}
var myi = document.createElement("i");
myi.setAttribute("class", "nav-icon " + item.icon);
myli.appendChild(myi);
var myp = document.createElement("p");
myp.setAttribute("onclick", 'menu("' + item.pagename + "," + roleid + '")');
myp.innerHTML = item.name + '<i class="right fa fa-angle-left"></i>';
myli.appendChild(myp);
myul.appendChild(myli);
}
});
return myul;
});
}
This is the calling code
document.getElementById("sidemenu").appendChild(menu("",8));
Edit Update: I have managed to get the error code to stop showing but the elements are still not showing. Here is the portion of html that should be receiving the elements.
<div id="sidemenu" class="sidebar">
<!-- Sidebar user (optional) -->
<div class="user-panel mt-3 pb-3 mb-3 d-flex">
<div class="image">
<img src="dist/img/vader.jpg" class="img-circle elevation-2" alt="User Image">
</div>
<div class="info">
<a href="#" class="d-block">Vader</a>
</div>
</div>
</div>
According the screenshot below the code is running and returning the elements to the sidemenu div but they are showing. I thought it might have been caused by the static div within the sidemenu div so I removed it and tried again and got the same results. I even cleared my browsing data just to make sure but still getting same scenario. My hat is off to you web developers.
Screenshot showing the elements
Edit 2: I have come to the conclusion it is in my call to the web api or the loop itself that is causing the issue. If I comment out the code like so
function menu(activemenu, roleid) {
var myul = document.createElement("ul");
var mynav = document.createElement("nav");
myul.setAttribute("class", "nav nav-pills nav-sidebar flex-column");
myul.setAttribute("data-widget", "treeview");
myul.setAttribute("role", "menu");
myul.setAttribute("data-accordion", "false");
mynav.setAttribute("class", "mt-2");
//
//var uri = 'api/menu';
//$.getJSON(uri)
// .done(function (data) {
// $('#sidemenu').empty();
// $.each(data, function (key, item) {
var myli = document.createElement("li");
//var n = String(item.roleid);
//var x = n.search(String(roleid));
//if (x > -1) {
// if (activemenu == item.pagename) {
// myli.setAttribute("class", "nav-item nav-link active");
// } else {
myli.setAttribute("class", "nav-item nav-link");
// }
var myi = document.createElement("i");
myi.setAttribute("class", "nav-icon fa fa-gear"); // + item.icon);
myli.appendChild(myi);
var myp = document.createElement("p");
myp.setAttribute("onclick", 'menu("' + "item.pagename" + '","' + roleid + '")');
myp.innerHTML = "This Page"; //item.name; // + '<i class="right fa fa-angle-left"></i>';
myli.appendChild(myp);
myul.appendChild(myli);
//}
// });
// //return myul;
mynav.appendChild(myul);
// console.log(mynav);
// });
return mynav;
}
I will get a menuitem to show up on the page. Time to play the uncomment game until I find the real issue.