+ Reply to Thread
Results 1 to 2 of 2




  

Thread: JavaScript Add-Remove HTML Elements with DOM

      
  1. #1
    Jedi Master JavaScriptBank is on a distinguished road
    Join Date
    July 10, 2009
    Posts
    167
    Rep Power
    3
    Feedback Score
    0

    Default JavaScript Add-Remove HTML Elements with DOM

    This JavaScript code example will dynamically add/remove HTML elements with content included within them according to ... detail at JavaScriptBank. com - 2.000+ free JavaScript codes

    [IMG]hxxp ://w w w.javascriptbank. com/javascript.images/snippet/javascript-add-remove-html-elements-with-dom.jpg[/IMG]
    Demo: JavaScript JavaScript Add-Remove HTML Elements with DOM

    How to setup

    Step 1: Place JavaScript below in your HEAD section
    JavaScript
    Code:
    <script type="text/javascript">
    // Created by: Dustin Diaz | hxxp ://w w w.dustindiaz. com/
    // This script downloaded from w w w.JavaScriptBank. com
    
    var Dom = {
      get: function(el) {
        if (typeof el === 'string') {
          return document.getElementById(el);
        } else {
          return el;
        }
      },
      add: function(el, dest) {
        var el = this.get(el);
        var dest = this.get(dest);
        dest.appendChild(el);
      },
      remove: function(el) {
        var el = this.get(el);
        el.parentNode.removeChild(el);
      }
    };
    var Event = {
      add: function() {
        if (window.addEventListener) {
          return function(el, type, fn) {
            Dom.get(el).addEventListener(type, fn, false);
          };
        } else if (window.attachEvent) {
          return function(el, type, fn) {
            var f = function() {
              fn.call(Dom.get(el), window.event);
            };
            Dom.get(el).attachEvent('on' + type, f);
          };
        }
      }()
    };
    Event.add(window, 'load', function() {
      var i = 0;
      Event.add('add-element', 'click', function() {
        var el = document.createElement('p');
        el.innerHTML = 'Remove This Element (' + ++i + ')';
        Dom.add(el, 'content');
        Event.add(el, 'click', function(e) {
          Dom.remove(this);
        });
      });
    });
    </script>
    Step 2: Copy & Paste HTML code below in your BODY section
    HTML
    Code:
    <div id="doc">
    
      <p id="add-element">Add Elements</p>
      <div id="content"></div>
    </div>





    JavaScript Line Graph script - JavaScript Virtual Keyboard - JavaScript Horizontal Slider

  2. #2
    Jedi Master mahesh10 is on a distinguished road
    Join Date
    June 17, 2010
    Location
    USA
    Posts
    239
    Rep Power
    2
    Feedback Score
    0

    Default Re: JavaScript Add-Remove HTML Elements with DOM

    Quote Originally Posted by JavaScriptBank View Post
    This JavaScript code example will dynamically add/remove HTML elements with content included within them according to ... detail at JavaScriptBank. com - 2.000+ free JavaScript codes

    [IMG]hxxp ://w w w.javascriptbank. com/javascript.images/snippet/javascript-add-remove-html-elements-with-dom.jpg[/IMG]
    Demo: JavaScript JavaScript Add-Remove HTML Elements with DOM

    How to setup

    Step 1: Place JavaScript below in your HEAD section
    JavaScript
    Code:
    <script type="text/javascript">
    // Created by: Dustin Diaz | hxxp ://w w w.dustindiaz. com/
    // This script downloaded from w w w.JavaScriptBank. com
    
    var Dom = {
      get: function(el) {
        if (typeof el === 'string') {
          return document.getElementById(el);
        } else {
          return el;
        }
      },
      add: function(el, dest) {
        var el = this.get(el);
        var dest = this.get(dest);
        dest.appendChild(el);
      },
      remove: function(el) {
        var el = this.get(el);
        el.parentNode.removeChild(el);
      }
    };
    var Event = {
      add: function() {
        if (window.addEventListener) {
          return function(el, type, fn) {
            Dom.get(el).addEventListener(type, fn, false);
          };
        } else if (window.attachEvent) {
          return function(el, type, fn) {
            var f = function() {
              fn.call(Dom.get(el), window.event);
            };
            Dom.get(el).attachEvent('on' + type, f);
          };
        }
      }()
    };
    Event.add(window, 'load', function() {
      var i = 0;
      Event.add('add-element', 'click', function() {
        var el = document.createElement('p');
        el.innerHTML = 'Remove This Element (' + ++i + ')';
        Dom.add(el, 'content');
        Event.add(el, 'click', function(e) {
          Dom.remove(this);
        });
      });
    });
    </script>
    Step 2: Copy & Paste HTML code below in your BODY section
    HTML
    Code:
    <div id="doc">
    
      <p id="add-element">Add Elements</p>
      <div id="content"></div>
    </div>





    JavaScript Line Graph script - JavaScript Virtual Keyboard - JavaScript Horizontal Slider
    Hi,
    Nice and useful information
    Thanks for sharing keep it up

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Amazing and Cool HTML Tooltips with JavaScript-jQuery
    By JavaScriptBank in forum HTML & Website Design
    Replies: 0
    Last Post: Jan 20th, 2011, 7:23 am
  2. Strip HTML tags in Rails, JavaScript and PHP
    By JavaScriptBank in forum HTML & Website Design
    Replies: 0
    Last Post: Nov 17th, 2010, 7:50 pm
  3. Selecting JavaScript HTML Elements Faster with jQuery
    By JavaScriptBank in forum HTML & Website Design
    Replies: 0
    Last Post: Oct 5th, 2010, 4:36 am
  4. Remove an Element in JavaScript Array
    By JavaScriptBank in forum HTML & Website Design
    Replies: 0
    Last Post: Sep 10th, 2010, 5:47 pm

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
WebTalkForums
WebTalkForums
Recent Forum Threads