Adding and removing a DIV in JavaScript
We shall create a function called createDiv(the div id, html content)
- We shall first create a new element with its type being a DIV createElement(‘div’)
- Then we set an attribute id, the id that is passed through
- Add the HTML content to the element
- and append it to the body of the document.
function createDiv (id, html) {
var newdiv = document.createElement(‘div’);
newdiv.setAttribute(‘id’, id);
if (html) {newdiv.innerHTML = html;} else {newdiv.innerHTML = ” “;}
document.body.appendChild(newdiv);}
To remove a div we shall create a function called removeDiv(the div id)
- Check to see if the DIV element exists
- If the element exists, find the parent of that element
- Then remove the element from the parent element
function removeDiv(div){
if (document.getElementById(div)) {
document.getElementById(div).parentNode.removeChild(document.getElementById(div));
}
}
About this entry
You’re currently reading “Adding and removing a DIV in JavaScript,” an entry on Dazzatechfind's Blog
- Published:
- May 22, 2010 / 11:46 am
- Category:
- javascript
- Tags:
- create div, element, javascript, remove div
No comments yet
Jump to comment form | comment rss [?] | trackback uri [?]