Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2004
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    clearbody() Function

    How to make a function that clears all text in body without clearing the one in the head (javascripts)

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 503 Times in 490 Posts
    maybe
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    <!--
    
    function Cal(selid,cbid,tbid){
     var val=1;
     if (document.getElementById(cbid).checked){
      val=document.getElementById(cbid).value;
     }
     document.getElementById(tbid).firstChild.data=(document.getElementById(selid).value*val).toFixed(2);
     document.getElementById(tbid+1).firstChild.data=document.getElementById(selid).value;
     document.getElementById(tbid+2).firstChild.data=(document.getElementById(selid).value*val)-document.getElementById(selid).value;
    }
    
    function NoTxt(){
     var bdy=document.getElementsByTagName('BODY')[0];
     var eles=bdy.getElementsByTagName('*');
     for (var zxc0 in eles){
      nds=eles[zxc0].childNodes;
      for (var zxc1 in nds){
       if (nds[zxc1].nodeType==3){
        nds[zxc1].parentNode.removeChild(nds[zxc1]);
       }
      }
     }
    }
    //-->
    </script></head>
    
    <body >
    <div>
    1. Is there a way <div><div>to show a subtotal and</div></div> tax amount in addition to the total?
    
    2. Is there a way to round decimals to two decimal places? For example 3000 + tax would be 268.13 instead of 268.125.
    
    3. Is there a way to display numbers using standard paragraph tags instead of a text input box?
    <select id="joe" onchange="Cal('joe','fred','tom');" >
    <option value="10" >10 ddddddddd</option>
    <option value="20" >20 ddddddddd</option>
    <option value="30" >30 ddddddddd</option>
    <option value="40" >40 ddddddddd</option>
    </select>
    <input id="fred" type="checkbox" value="1.1" onclick="Cal('joe','fred','tom');"><br>
    subtotal <span id="tom1"   >&nbsp;</span><br>
    tax amount <span id="tom2"   >&nbsp;</span><br>
    total <span id="tom"   >&nbsp;</span><br>
    <input type="button" name="" value="Remove Text" onclick="NoTxt();"></body>
    <script> vic=0; </script>
    <form name=Show id=Show style="position:absolute;visibility:visible;top:450px;left:0px;" >
    <input size=10 name=Show1 >
    <input size=10 name=Show2 >
    <input size=10 name=Show3 >
    <input size=10 name=Show4 >
    <input size=10 name=Show5 >
    <input size=10 name=Show6 >
    </form>
    </div>
    </html>

  • #3
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    <html>

    <head>
    <script language="javascript">
    function clearBody(clearedElement){
    clearedElement.innerHTML = '';
    }
    </script>
    </head>

    <body>

    <input type="button" onclick="clearBody(document.body);" value="clear">

    <table>
    <tr><td>1asfqwfuihqweiofuhqweuiofh</td><td>1asfqwfuihqweiofuhqweuiofh</td><td>1asfqwfuihqweiofuhqweuiofh</td></tr>
    <tr><td>1</td></tr>
    <tr><td>1</td></tr>
    </table>

    </body>

    </html>

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    In browsers that support it, doing a shallow copy of the body element and replacing the original with the copy is probably the best performant way of doing it with W3C DOM. Or even replacing it with a newly created body element...
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    New Coder
    Join Date
    Aug 2004
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Never mind... I tried this and it works perfectly...
    Code:
    function clearbody () {
    	document.body.innerHTML="";
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •