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.
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.
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.
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.
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.
$("#1").empty();
Also you shouldnt really start id's with a number
$("#1").html("");
or just
$("#1").html("your new content");
change id to start w/a letter
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.
$(document).ready(function()
{
$('div#one').children().remove();
});
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