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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    add/remove div's question

    Hey everyone,

    I am new to JS and this forum

    Was hoping i could get some help using some code i am trying to get working on my site. I am using Justin Diaz's add and remove form elements code. I have it working but i want to get it working on the same page with twice and i was just wondering what i have to change to get it working

    here is the code so anyone can see

    Code:
    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);
              });
            });
          });

    And html

    Code:
    <div id="doc">
          <h1>Add &amp; Remove Elements with JavaScript</h1>
          <p id="add-element">Add Elements</p>
          <div id="content"></div>
        </div>
    Any help would be awesome....thanks!!
    Last edited by Touch; 02-04-2009 at 05:37 AM. Reason: Bad Title

  • #2
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Solved

    Sorry everyone. i figured out what i needed to do.


  •  

    Posting Permissions

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