Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the Contents of a Div using JQuery

Tags:

jquery

How to remove the contents of a Div Using JQuery. For eg.

 <div id="1">
  <label id="label1"/><br/>
  <input type="text" data-attr="phoneEuro" style="width: 200px;" id="input1"/> <label id="instr1"/>
 </div>

i want to remove the full content of the Div . and to add new contents to it.

like image 672
Mercy Avatar asked Jul 17 '09 06:07

Mercy


People also ask

How do I clear content of a div?

To clear the contents of a div element, set the element's textContent property to an empty string, e.g. div. textContent = '' . Setting textContent on the element removes all of its children and replaces them with a single text node of the provided value.

How check div is empty or not in jQuery?

Method 1: Using the “:empty” selector: The element to be checked using is() method. The is() method is used to check if one of the selected elements matches to the selector element. This method can be used on this element to test if it is empty by using “:empty” selector.

How remove and append in jQuery?

jQuery uses: . append(); and . remove(); functions to accomplish this task. We could use these methods to append string or any other html or XML element and also remove string and other html or XML elements from the document.

What does addClass do in jQuery?

jQuery addClass() Method The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.


4 Answers

$("#1").empty();

Also you shouldnt really start id's with a number

like image 197
Darko Z Avatar answered Oct 06 '22 08:10

Darko Z


$("#1").html("");

or just

$("#1").html("your new content");

change id to start w/a letter

like image 39
Jason Avatar answered Oct 06 '22 09:10

Jason


First, according to the spec, id must start with a letter.

 <div id="myDiv">
  <label id="label1"/><br/>
  <input type="text" data-attr="phoneEuro" style="width: 200px;" id="input1"/> <label id="instr1"/>
 </div>


$('#myDiv').html('your new content');

If you use html method, you can simply override the existing DIV content.

like image 21
SolutionYogi Avatar answered Oct 06 '22 08:10

SolutionYogi


$(document).ready(function()
{
    $('div#one').children().remove();
});
like image 37
Keith Donegan Avatar answered Oct 06 '22 09:10

Keith Donegan