Results 1 to 2 of 2

Thread: Remove on click

  1. #1
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default Remove on click

    Hi I have a simple code for adding and removing elements.
    My problem is that I need only to have the option to remove elements and it should onload print me 5 elements (and not include the option to add more..)

    Thanks in advanced

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <
    title>ADD N REMOVE</title>
     <
    script type="text/javascript">
          var 
    Dom = {
            
    get: function(el) {
              if (
    typeof el === 'string') {
                return 
    document.getElementById(el);
              } else {
                return 
    el;
              }
            },
            
    add: function(eldest) {
              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(
    eltypefn) {
                  
    Dom.get(el).addEventListener(typefnfalse);
                };
              } else if (
    window.attachEvent) {
                return function(
    eltypefn) {
                  var 
    = function() {
                    
    fn.call(Dom.get(el), window.event);
                  };
                  
    Dom.get(el).attachEvent('on' typef);
                };
              }
            }()
          };
          
    Event.add(window'load', function() {
            var 
    0;
            
    Event.add('add-element''click', function() {
              var 
    el document.createElement('p');
              
    el.innerHTML 'Remove This Element (' + ++')';
              
    Dom.add(el'content');
              
    Event.add(el'click', function(e) {
                
    Dom.remove(this);
              });
            });
          });
        
    </script>
    </head>

    <body>
    <div id="doc">
          <h1>Add &amp; Remove Elements with JavaScript</h1>
          <p id="add-element">Add Elements</p>
         <div id="content"></div>
        </div>
    </body>
    </html> 

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Change this part:
    Code:
     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); 
              }); 
            }); 
          });
    to:
    Code:
     Event.add(window, 'load', function() { 
            var i = 0; 
    	for(var n=1;n<=5;n++){
              var el = document.createElement('p'); 
              el.innerHTML = 'Remove This Element (' + ++i + ')'; 
              Dom.add(el, 'content'); 
              Event.add(el, 'click', function(e) { 
                Dom.remove(this); 
              });
    		  }
          });
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. The Following User Says Thank You to rangana For This Useful Post:

    d-machine (08-30-2008)

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
  •