Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DOM, NODES & HTML 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Not sure if this should be in the Javascript forum, but here goes....

I've been playing with adding & deleting ChildNodes to Document nodes by ID.

E.G. set a div tag with an ID, then using appendChild to add a paragraph node, using something like this
Code:
var theNewParagraph = document.createElement('p');
var theTextOfTheParagraph = document.createTextNode('Some content.');
theNewParagraph.appendChild(theTextOfTheParagraph);
document.getElementById('someElementId').appendChild(theNewParagraph);

However I have a couple of questions I can't find an answer to,

1. how do I give the new paragraph an ID so I can then issue style attributes to change font colour, size, etc...

2. document.createTextNode seems to place just text in the paragraph, even if the text you supply is HTML. How do you change the content of a paragraph node with HTML and have the browser interpret the code rather than showing the HTML on the screen as text.

All help is appreciated.

Regards,
1DMF

 
Use this code technique to add an ID to your element:
Code:
var theNewParagraph = document.createElement('p');
theNewParagraph.id='myFunkyID';
And this will let you set HTML (instead of text):
Code:
var theNewParagraph = document.createElement('p');
theNewParagraph.id='myFunkyID';
document.getElementById('someElementId').appendChild(theNewParagraph);
document.getElementById('myFunkyID').innerHTML = '<b>Bold</b>';
In an ideal world you would add each node... using innerHTML is a shortcut (not standard) that currently does exactly what you ask.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
spot on, thanks Jeff.

looks like some aren't going to like inner.HTML if it isn't W3C, do you know if this is cross browser compatible but just not "Standard" or is it browser specific?
 
Well, it's pretty much a defacto standard now anyway I guess. It was originally something mooted by Microsoft and incorporated in IE. It's now supported in all major browsers (that I am aware of).

Here are 2 good links on this topic (the second shows the performance differences in using the DOM methods vs innerHTML):


Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
hmm, looks like something MS has done the industry actually adopted, wonders will never cease.

The HTML contained by a tag, as a string.

Originally a Microsoft extension, innerHTML is so useful that all other browsers support it, too.

interesting the outer.HTML test though, once you click, it changes the node content, but then the inner.HTML no longer works.

oh what fun us techies have, i'm sure I should really go get a real job and a life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top