I am trying to fill a table dynamically, depending on the users in my database. For each user I want a dropdown option in order to change his role. I use bootstrap for this dropdown menu, hence I include:
<script src=".\..\javascripts\jquery.js"></script>
<script src=".\..\plugins\bootstrap-4.4.1-dist\js\bootstrap.bundle.min.js"></script>
I already have the functionality of this dropdown menu in a different ejs file, but there it is only static. It looks like that:
<div class="dropdown show">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
User
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" id="Admin" href="#">Admin</a>
<a class="dropdown-item" id="User" href="#">User</a>
<a class="dropdown-item" id="Maintainer" href="#">Maintainer</a>
</div>
</div>
This code is fully functional. Now I am trying to apply the same approach dynamically, but I fail:
//Button for Role
let roleButtonCell = row.insertCell(i);
i++;
//Div for bootstrap for Role Button
var tempDivRole = document.createElement("div");
tempDivRole.className = "dropdown show";
//Role change and display
var tempButtonRole = document.createElement("a");
tempButtonRole.setAttribute("role", "button");
tempButtonRole.className = "btn btn-secondary dropdown-toggle"; //ClassName for bootstrap
tempButtonRole.setAttribute("href", "#");
tempButtonRole.setAttribute("id", "dropdownMenuLink");
tempButtonRole.setAttribute("data-toggle", "dropdown");
tempButtonRole.setAttribute("aria-haspopup", "true");
tempButtonRole.setAttribute("aria-expanded", "false");
tempButtonRole.innerHTML = `${user.role}`;
//DropDown Options for roles
var tempDropdownRoles = document.createElement("div");
tempDropdownRoles.className ="dropdown-menu";
tempDropdownRoles.setAttribute("aria-labelledby", "dropdownMenuLink");
var tempDropdownItemAdmin = document.createElement("a");
tempDropdownItemAdmin.className = "dropdown-item";
tempDropdownItemAdmin.setAttribute("href", "#");
tempDropdownItemAdmin.innerHTML = `Admin`;
var tempDropdownItemUser = document.createElement("a");
tempDropdownItemUser.className = "dropdown-item";
tempDropdownItemUser.setAttribute("href", "#");
tempDropdownItemUser.innerHTML = `User`;
var tempDropdownItemMaintainer = document.createElement("a");
tempDropdownItemMaintainer.className = "dropdown-item";
tempDropdownItemMaintainer.setAttribute("href", "#");
tempDropdownItemMaintainer.innerHTML = `Maintainer`;
tempDropdownRoles.appendChild(tempDropdownItemAdmin, tempDropdownItemUser, tempDropdownItemMaintainer);
//Add Button Edit to div and div to cell
tempDivRole.appendChild(tempButtonRole, tempDropdownRoles);
roleButtonCell.appendChild(tempDivRole);
With this code I get the same situation as in Uncaught TypeError: Cannot read property 'setAttribute' of undefined at Object.onLoad. Hence I removed
tempButtonRole.setAttribute("data-toggle", "dropdown");
But with or without this line, the dropdown menu does not work: When clicking on the button, no dropdown is displayed. Instead of the element "a", I tried "select":
var tempButtonRole = document.createElement("select");
Now the dropdown appears, but the options are empty and even the innerHTML of tempButtonRole is not displayed, the button is empty. Am I missing any attributes or what am I doing wrong? As mentioned above, the static approach works perfectly fine.