www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Removing created element: error message

  1. #1
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,356

    resolved [RESOLVED] Removing created element: error message

    I am trying to understand the following code.
    It appears to create the new element with no errors.

    However, when I click to remove the element, I get this 'warning' message in the error console:

    Timestamp: 5/8/12 4:40:44 PM
    Warning: Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.
    Source File: file:///Users/JMR/Desktop/Blank.html
    Line: 1

    Here is the code I'm using on a local computer (not on server yet).
    Can anyone tell me how to eliminate the warning message in the error console?
    As far as I can tell, I am using the getElementById function.

    Code:
    <html>
    <head>
    <title>Add / Remove Element</title>
    <script type="text/javascript">
    this.num = 1;
    function addElement(){
      var $top = document.getElementById('top');
      var newId = document.createElement('div');
      var id = 'my'+this.num;
      newId.setAttribute('id', id );
      var str = "<a href='' onclick='removeThis( "+id +");return false'>";
      str += "Remove this element: "+id+"</a>";
      newId.innerHTML = str;
      $top.appendChild(newId);
      this.num++;
    }
    function removeThis( id ){
      var d = document.getElementById('top');
      d.removeChild(id);
    }
    </script>
    </head>
    <body>
    <input type="button" name="button" value="Add Element" onclick="addElement()" />
    <div id="top" ></div>
    </body>
    </html>

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Add / Remove Element</title>
    <script type="text/javascript">
    this_num = 1;
    function addElement(){
      var $top = document.getElementById('top');
      var newId = document.createElement('div');
      var id = 'my'+this_num;
      newId.setAttribute('id', id );
      var str = "<a href='' onclick='removeThis(this);return false'>";
      str += "Remove this element: "+id+"</a>";
      newId.innerHTML = str;
      $top.appendChild(newId);
      this_num++;
    }
    function removeThis(obj){
      //var d = document.getElementById('top');
      //d.removeChild(id);
      var el = obj.parentNode;
      el.parentNode.removeChild(el);
    }
    </script>
    </head>
    <body>
    <input type="button" name="button" value="Add Element" onclick="addElement()" />
    <div id="top"></div>
    </body>
    </html>
    Last edited by Padonak; 05-08-2012 at 04:30 PM.
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,356

    Thumbs up Thanks...

    Thank you, that did the trick.

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    there were no any tricks. i don't know the english form for it, but in russian they say 'you have your eye soaped up' - it means that when you looking at something similar for long time you become less attentive. that's why it seems easy to fix somebody's else code but your own code never wants to work. every time when i face such a problem i just stop coding and go play some game or watch a movie and when i come back all the defects are easily detected ))
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,356

    Thumbs up

    Quote Originally Posted by JMRKER View Post
    Thank you, that did the trick.
    English slang for "it solved the problem".

Thread Information

Users Browsing this Thread

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

Posting Permissions

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



Recent Articles