admin管理员组

文章数量:1277566

Im using PRE tag of HTML and I need on click to clear the content of it, I've tried the following without success.

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

In the index html I use it like this

<pre id="display"></pre>

Im using PRE tag of HTML and I need on click to clear the content of it, I've tried the following without success.

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

In the index html I use it like this

<pre id="display"></pre>
Share Improve this question asked Feb 9, 2016 at 12:10 user4445419user4445419 3
  • 1 tried .html("") or .text("") – Jaromanda X Commented Feb 9, 2016 at 12:11
  • .empty() too is there.....:) – Jai Commented Feb 9, 2016 at 12:13
  • 1 If you are using any library/framework methods do try to see their documention first. It's easy to know what to expect from specific method. – Diljohn5741 Commented Feb 9, 2016 at 12:14
Add a ment  | 

6 Answers 6

Reset to default 3

Use html("") to clear content

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

If you are using jquery then use:

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

Make sure you are loading jquery.

Try using simple javascript

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

Hope this helps.

You used $('selector').val('') and it is for input boxes, and won't work here!

I suggest you to use one of the bellow ways:

$(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./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>

this clear the data between the tag

$('#display').text('');
$('#display').html("");

Here is the working fiddle example.
https://fiddle.jshell/75fLkhmz/

本文标签: javascriptHow to clear pre tag in htmlStack Overflow