I set a popover with a content that contains table with thead
and tbody
with its unique id. If I manually write document.getElementById("servicesEx-popTable_content");
on browser console, the console shows me the element. But When I set its innerHTML
dynamically, the error says Cannot read property 'innerHTML' of null.
When I check the checkbox, A row should be inserted in tbody
and when I uncheck the checkbox, inserted row should be removed.
document.addEventListener("click", function(x) {
if (x.target && x.target.id === "cbox") {
if (x.target.checked === true) {
var t = document.getElementById("servicesEx-popTable_content");
t.innerHTML += "<tr><td id='popover-1'></td>some name<td>1</td><td>$50</td></tr>";
} else {
var t = document.getElementById("popover-1");
t.parentNode.removeChild(t);
}
}
});
$(function() {
$('[data-toggle="popoverEx"]').popover({
container: 'body',
placement: 'bottom',
html: true,
viewport: 'body',
content: function() {
return "<table class='table services-popTable'><thead><tr><th>Name</th><th>Quantity</th><th>Price</th></tr></thead><tbody id='servicesEx-popTable_content'></tbody></table>";
}
});
});
<!DOCTYPE html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>
<div>
<span style="cursor: pointer;" data-toggle="popoverEx" data-content="">Click to see popover</span>
<label for="cbox">Click me</label>
<input type="checkbox" id="cbox">
</div>
</body>