Remove data from a div with Jquery

Asked

Viewed 9,717 times

0

I need to remove the information inside one or remove the div from my web page with Jquery, I tried using the code:

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

and also

    $("#myid").remove();

none of the solutions worked, there is some other way?

  • 1

    You can display the HTML you have and want to remove?

  • You can call $("#myid"). Empty(). Empty() removes all content that is inside maintaining the element itself.

  • It worked using the $("#myid") method. Empty().

1 answer

1


Here are some examples of how to remove a div or remove the contents thereof:

1st Remove the div by clicking button using the method remove():

$("#removerDiv").click(function(){
    $('#divteste').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="divteste" style="border:1px solid">
	<p>Conteúdo da DIV a ser removida</p>
</div>

<br>
<button id="removerDiv" type="button">Remover DIV</button>

2º Remove the contents of div keeping the same when clicking button with the method html(""):

$("#removerConteudo").click(function(){
	$('#divteste').html("");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="divteste" style="border:1px solid">
	<p>Conteúdo da DIV</p>
</div>
<br>
<button id="removerConteudo" type="button">Remover Conteúdo</button>

3º Remove the contents of div keeping the same when clicking button with the method empty():

$("#removerConteudo").click(function(){
	$('#divteste').empty();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="divteste" style="border:1px solid">
	<p>Conteúdo da DIV</p>
</div>
<br>
<button id="removerConteudo" type="button">Remover Conteúdo</button>

In the three examples above, I called the function to remove the div or remove the content from it through id of div. In jQuery, to call an element by id the syntax is used #idDoElemento.

I believe your code didn’t work because you forgot to import the library jQuery within the tag head page:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.