Seppo Kuusela Seppo Kuusela - 2 months ago 21
CSS Question

Jquery remove text/numbers inside of div

i have started learning JQuery and made script that when you press button it adds that button value into a div. I made a CLEAR text that when you press it clears the text / numbers from that div but the problem is when you press the CLEAR and i press button that should add X value into that div it does not print it and i have to refresh the page.

CLEAR SCRIPT

$(document).ready(function(){
$("p").click(function(){
$(".total").remove();
});
});


CLEAR TEXT

<p>CLEAR</p>


DIV

<div id="div_number" class="total"></div>


EDIT:

I also have to remove
value=""
from this
Oh it can be my mistake, i also have to remove the value="" from
``

EDIT 2:

This is the code that i have made jsfiddle.net/utcs4L1h/

Answer

Update

So, based on your provided code on jsfiddle, you just need to add this: count = 0; after this: $('.total').empty();

$(".clear").click(function() {
    $('.total').empty();
    count = 0;
});

I kept following for future readers

You used remove(), instead of empty() or just text('').

From jQuery documentation:

  • .remove( [selector ] ): Remove the set of matched elements from the DOM.
  • .empty(): Remove all child nodes of the set of matched elements from the DOM.
  • text(): Get the combined text contents of each element in the set of matched elements, including their descendants.

Read more about remove(), empty() also text()