Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear the text contents of a div in javascript?

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?

like image 640
Ben Avatar asked Jun 14 '10 17:06

Ben


People also ask

How do I clear text inside a div?

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.

How do I remove all elements from a div?

The empty() method removes all child nodes and content from the selected elements.

How do you clear an element in JavaScript?

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 .

How do you hide text in JavaScript?

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").


1 Answers

You can use $("#tempDiv").empty().

like image 172
Ken Redler Avatar answered Oct 24 '22 04:10

Ken Redler