Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: [DHTML] Removing parts from the DOM using classes

  1. #1
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default [DHTML] Removing parts from the DOM using classes

    1) CODE TITLE: Removing parts from the DOM using classes

    2) AUTHOR NAME/NOTES: Arie Molendijk

    3) DESCRIPTION: It is relatively easy to add elements to the DOM. But removing whole parts may be time-consuming if it's done with references to IDs. This script does it with classes. (If this is already known, don't read it).

    4) URL TO CODE: http://molendijk.110mb.com/RemoveFromDom/dom.html

    or, ATTACHED BELOW (see #3 in guidelines below):

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    The script contained a serious error. I corrected it. Please re-download if you downloaded before January 7th.

    Arie Molendijk.

  3. #3
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    A little refractoring
    Code:
    function removeFromDom(tag, className) {
      var els = document.getElementsByTagName(tag), n = els.length, el;
      for(var i = n - 1; i >= 0; --i)) {
        if(els[i].className == className) {
          var el = els[i];
          el.style.color = "#f00"; // I don't understand this . . .
          while(el.hasChildNodes()) 
            el.removeChild(el.firstChild);
        }
      }
    }
    Last edited by Trinithis; 01-07-2008 at 06:08 AM.
    Trinithis

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    A little more refactoring:
    Code:
    function clearByClass(tag, className) {
      for(var re = new RegExp('(?:^|\\s)' + className + '(?:$|\\s)'), el, els = document.getElementsByTagName(tag), i = els.length - 1; i >= 0 && el = els[i]; --i)
        if(re.test(el.className))
          while(el.hasChildNodes()) 
            el.removeChild(el.firstChild);
    }
    Here's an actual removeFromDom() function:
    Code:
    function removeFromDom(tag, className) {
      for(var re = new RegExp('(?:^|\\s)' + className + '(?:$|\\s)'), el, els = document.getElementsByTagName(tag), i = els.length - 1; i >= 0 && el = els[i]; --i)
        if(re.test(el.className))
          el.parentNode.removeChild(el);
    }
    Last edited by Twey; 01-07-2008 at 08:18 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Hello Trinithis, Twey,

    I tried your functions on my page, but they don't work. The ErrorConsole says that removeFromDom (or clearByClass) are not defined.

    Arie.

  6. #6
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Trinithis,
    This is as close as I can get to your function. It works fine and is short:
    Code:
    <script type="text/javascript">
    var d=document //needed elsewhere
    function remove_from_dom(tag,className) {
    var els = document.getElementsByTagName(tag)
    	for (i=0;i<els.length; i++) {
    		if (els.item(i).className == className){
    		while (els.item(i).firstChild)
    		els.item(i).removeChild(els.item(i).firstChild);
    	}
           }
          }
    </script>
    Arie
    Last edited by molendijk; 01-07-2008 at 11:47 AM. Reason: Correction

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I tried your functions on my page, but they don't work. The ErrorConsole says that removeFromDom (or clearByClass) are not defined.
    Then I suspect that you're doing something wrong. There was an extra bracket (now removed), but if your error console didn't report that as well then you're including the script incorrectly somehow.
    This is as close as I can get to your function. It works fine and is short:
    But that still doesn't remove the items with that class. It just empties them of children. That's why the dual functions in my post: one does what your function does, and the other does what it says it does.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Twey, now I get 'Index or size is negative or greater than the allowed amount'.

    The whole problem may be that the function is supposed to operate on tags that already exist on the page (that are not created/added).

    Arie
    Last edited by molendijk; 01-07-2008 at 01:21 PM.

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Hmm? It works fine here. Demo page?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Twey, I copied my URL-TO-CODE page, and put both your and my functions in it. It's here. If you choose to click on your link first (removeFromDom), then refresh the page before clicking on my link (remove_from_dom).
    Thanks,
    Arie.

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
  •