How to clear pre tag in html

I use the HTML PRE tag and I need to click to clear its contents. I tried the following without success.

$('#display').clear;
$('#display').val("");

In the html index, I use it like this:

<pre id="display"></pre>
+4
source share
6 answers

You used $('selector').val(''), and this is for input fields, and will not work here!

I suggest you use one of the following methods:

$(document).ready(function(){

$("#delete-1").click(function(){
  $('#display1').html('');
});

$("#delete-2").click(function(){
  document.getElementById('display2').innerHTML ="";
});


$("#delete-3").click(function(){
  $('#display3').empty();
});

  
$("#delete-4").click(function(){
  $('#display4').text('');
});

  
  
 



})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="display1">
  
  Here is pre content
</pre>

<br/>
<pre id="display2">
  
  Here is pre content
</pre>

<br/>
<pre id="display3">
  
  Here is pre content
</pre>
<br/>
<pre id="display4">
  
  Here is pre content
</pre>

<br/>
<a id="delete-1" href="#">Delete #1</a>
<a id="delete-2" href="#">Delete #2</a>
<a id="delete-3" href="#">Delete #3</a>
<a id="delete-4" href="#">Delete #4</a>
Run codeHide result
+2
source

Use html("")to clear contents.

 $('#display').html("");
+4
source

jquery, :

<script>
$( "#display" ).empty();
</script>

, jquery.

+2

javascript

document.getElementById('display').innerHTML ="";

, .

+1
$('#display').html("");

.
https://fiddle.jshell.net/75fLkhmz/

+1

$('#display').text('');
+1

Source: https://habr.com/ru/post/1627953/


All Articles