/    Sign up×
Community /Pin to ProfileBookmark

[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>
[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@PadonakMay 08.2012 — [CODE]<!DOCTYPE html>
<html>
<head>
<title>Add / Remove Element</title>
<script type="text/javascript">
this[COLOR="Red"][B]_[/B][/COLOR]num = 1;
function addElement(){
var $top = document.getElementById('top');
var newId = document.createElement('div');
var id = 'my'+this[COLOR="red"][B]_[/B][/COLOR]num;
newId.setAttribute('id', id );
var str = "<a href='' onclick='removeThis([COLOR="red"][B]this[/B][/COLOR]);return false'>";
str += "Remove this element: "+id+"</a>";
newId.innerHTML = str;
$top.appendChild(newId);
this[COLOR="red"][B]_[/B][/COLOR]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>[/CODE]
Copy linkTweet thisAlerts:
@JMRKERauthorMay 08.2012 — Thank you, that did the trick. ??
Copy linkTweet thisAlerts:
@PadonakMay 09.2012 — 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 ))
Copy linkTweet thisAlerts:
@JMRKERauthorMay 09.2012 — Thank you, that did the trick. ??[/QUOTE]

English slang for "it solved the problem".

?
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...