SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member nacker4it's Avatar
    Join Date
    Mar 2008
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Removing some tags using javascript from an html file

    I am not good in javascript but i need to replace or remove/hide some tags from html code, i have code but only one line of code is working very well but last two lines are not, i don't know what's wrong with it. I need help someone?

    My script is:

    HTML Code:
    <script language="javascript">
    document.body.innerHTML = document.body.innerHTML.replace('Hello Worlds', '');
    document.body.innerHTML = document.body.innerHTML.replace('<input type="checkbox" name="Usecheck" value="1"><Strong>Click here</Strong>', '');
    document.body.innerHTML = document.body.innerHTML.replace('<img src="images/button.gif" bordor=0>', '');
    </script>
    This line works good:
    HTML Code:
    document.body.innerHTML = document.body.innerHTML.replace('Hello Worlds', '');
    But these lines are not working:
    HTML Code:
    document.body.innerHTML = document.body.innerHTML.replace('<input type="checkbox" name="Usecheck" value="1"><Strong>Click here</Strong>', '');
    document.body.innerHTML = document.body.innerHTML.replace('<img src="images/button.gif" bordor=0>', '');
    Advance Thanks for help

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    What does the html code look like.
    If the pieces that you want to remove are in the same container, then it's as easy as

    Code javascript:
    var container = document.getElementById('myContainer');
    while (container.firstChild) {
        container.removeChild(container.firstChild);
    }

    But there's no need to do even that. You can just hide the elements from view instead with

    Code javascript:
    document.getElementById('myContainer').style.display = 'none';
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably the innerHTML is returning your tagnames in upper case,
    so they aren't matching your replace text.

    innerHTML returns what the browser renders the page as, not the literal source text.
    If you are using IE it probably strips the quotes from some of the attribute values as well, and who knows what else.

  4. #4
    SitePoint Member nacker4it's Avatar
    Join Date
    Mar 2008
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried these examples but failed. I hope there is not element id in html code the only tags are available.

    I believe it is pretty simple to using "document.body.innerHTML.replace" replace function with empty quotes ""

  5. #5
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nacker4it View Post
    I am not good in javascript but i need to replace or remove/hide some tags from html code, i have code but only one line of code is working very well but last two lines are not, i don't know what's wrong with it.
    .innerHTML is at best a convenient way of creating visual content; it should not be used for element manipulation.

    As you have been told already, your code relies on matching the existing markup exactly, which is very uncertain without using regular expressions.

    You must address each element by its id, and either hide or remove it using DOM methods.

    This function removes the elements whose IDs are passed to it:
    Code:
    <html>
    <head>
    <title>Remove Elements</title>
    
    <script type='text/javascript'>
    
    function strip()
    {
     for(var i=0, len=arguments.length, ref; i<len; i++)
      if( (ref=document.getElementById(arguments[i])) && ref!=ref.parentNode)
       ref.parentNode.removeChild(ref);
    }
    
    </script>
    </head>
    
    <body>
    <p id='hw'>Hello Worlds</p>
    <img id='theImage' src="images/button.gif" border=0 alt="IMAGE HOLDER">
    <form>
     <div id='cbHolder'>
      <input  type="checkbox" name="Usecheck" value="1"><Strong>Click here</Strong>
     </div>
     <input type="button" value='Remove' onclick="strip('cbHolder', 'hw', 'theImage')">
    </form>
    </body>
    </html>
    Tab-indentation is a crime against humanity.

  6. #6
    SitePoint Member nacker4it's Avatar
    Join Date
    Mar 2008
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes this code works on div tag but my actual code doesn't have div and i want to remove this <image> and <input> without div tag...

    any solutions?

  7. #7
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nacker4it View Post
    Yes this code works on div tag but my actual code doesn't have div and i want to remove this <image> and <input> without div tag...

    any solutions?
    It works on any tag, I just added the div for convenience. Why don't you actually try it?
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

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