Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear HTML page using JavaScript

Is there any way to delete portions of existing content on a page with a JavaScript function?
Like this:

<I WANT THIS CLEARED>
<IS THERE ANY JAVASCRIPT OR HTML STATEMENT I CAN PUT HERE?>
<I WANT THIS DISPLAYED>

What should I put in <IS THERE ANY JAVASCRIPT OR HTML STATEMENT I CAN PUT HERE?>?

I know putting <div style="display:none"><plaintext> after the part I want displayed will hide the end of a page, but how do I clear the beginning?

like image 802
Adrian Chang Avatar asked Dec 01 '09 22:12

Adrian Chang


People also ask

Is there a clear function in JavaScript?

clear() function in JavaScript. The clear() function of the Set object removes all elements from the current Set object.

How do you empty a container in JavaScript?

To clear the contents of a div element, set the element's textContent property to an empty string, e.g. div. textContent = '' .


1 Answers

You can use document.body.innerHTML = "". It will take out everything in the body so far as the browser processes the page. See the following example:

<html>
    <head></head>
    <body>
        <p>This should not appear</p>

        <script type="text/javascript">
            document.body.innerHTML = '';
        </script>

        <p>This should appear.</p>
    </body>
</html>

This is, however, not a great way to accomplish this. If you want some text hidden, consider jQuery as others have mentioned.

like image 55
EndangeredMassa Avatar answered Oct 22 '22 17:10

EndangeredMassa