I'm temporarily adding contents to a div,tempDiv, and adding that to a link which is appended to a div, #contentHere, that's displayed. I need to clear the contents of tempDiv so that the links aren't appended to each other, creating a string of urls that don't link to anywhere.
$(document).ready(function(){
$.getJSON("data.php", function(data){
for(i = 0; i < 5; i++){
$("#tempDiv").append(data.justIn[i].dataLink+ ' ');
$("#contentHere").append("<a href=\"#tempDiv\">Click to go to the div link</a>");
//I need to clear the contents of tempDiv here
}
});
});
Solutions to clearing the temporary contents of the div as I go?
JavaScript provides the functionality of clearing the content of div. There are two methods to perform this function, one by using innerHTML property and other by using firstChild property and removeChild() method.
The empty() method removes all child nodes and content from the selected elements.
Setting textContent to empty StringBy setting an empty string as textContent , we can remove all the children of an Element. When we set a value to textContent , JavaScript will replace all children of the element with a #text node. This method is considered faster than innerHTML .
Style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”. document. getElementById("element").
You can use $("#tempDiv").empty()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With