/    Sign up×
Community /Pin to ProfileBookmark

Creating Tag Elements HELP

Hi Guys,

I am having trouble with the following code:

[B]Line 1:[/B] var newP = document.createElement(“font”);
[B]Line 2:[/B] newP.id = “myNewP”;
[B]Line 3:[/B] window.alert(document.getElementById(“myNewP”).id);

I am having trouble with [B]Line 3[/B] in the code. It does not display the alert window which indicates there is something wrong with the code: [B]document.getElementById(“myNewP”).id[/B] because if I use [B]newP.id[/B] instead, it works.

Therefore my question is this: Is [B]newP[/B] equivalent to [B]document.getElementById(“myNewP”)[/B] ?

Please help me!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@felgallMar 20.2007 — You have to add the tag into the web page before trying to retrieve it. So far all you have done is create a deprecated tag and give it an id.

Try this:

var newP = document.createElement("p");

newP.id = "myNewP";

var theBody = document.getElementsByTagName('body')[0];

theBody.appendChild(newP);

window.alert(document.getElementById("myNewP").id);
Copy linkTweet thisAlerts:
@KorMar 20.2007 — or if you need an inline element, create the SPAN element
×

Success!

Help @ectobuilder 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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