appendchild element once if element is not present in javascript code example
Example 1: add element to body javascript
var elem = document.createElement('div');
elem.style.cssText = 'position:absolute;width:100%;height:100%;opacity:0.3;z-index:100;background:#000';
document.body.appendChild(elem);
Example 2: for each append to document
var person = {
firstName: "john",
lastName: "doe",
age: 45,
placeOfBirth: "somewhere"
}
for(var key in person) {
var p = document.createElement("p");
p.innerHTML = person[key];
document.body.appendChild(p)
}
Example 3: appendchild element once if element presense in js
<script>
var button = false;
function myfun(){
if ( button === false){
var b1 = document.createElement("BUTTON");
b1.innerHTML = "Click Me";
document.body.appendChild(b1);
return button = true;
}
}
</script>