Can JS place html code into a DIV ????

T

Tom Szabo

Hi,

What I like to know is if it is possible to have a JavaScript writing code
into a DIV section, that is say during load or on a certain event in the
same page it would insert the word "hello"

----------------
.......
<script>
function insert_hello(){
?????
}
</script>

<body onload="insert_hello()"....

<div id=div1 name=div1>

</div>
....

-----------------

...so it would display the word "hello" in the area taken up by the <DIV
id=div1 name=div1>
I understand you can change attributes/properties like the value of a text
field but what about this scenario???


TIA,

Tom
 
D

Daniel Kirsch

Tom said:
Hi,

What I like to know is if it is possible to have a JavaScript writing code
into a DIV section, that is say during load or on a certain event in the
same page it would insert the word "hello"

----------------
......
<script>
function insert_hello(){
?????
}
</script>

<body onload="insert_hello()"....

<div id=div1 name=div1>

</div>
...

-----------------

You may use innerHTML which isn't a W3 standard but is supported by
nearly all browsers out there:

function insert_hello(){
var yourElm = document.getElementById("div1");
if (yourElm)
yourElm.innerHTML = "Hello<br>everybody";
}

Daniel
 
T

Tom Szabo

thanks, working well!


Daniel Kirsch said:
You may use innerHTML which isn't a W3 standard but is supported by
nearly all browsers out there:

function insert_hello(){
var yourElm = document.getElementById("div1");
if (yourElm)
yourElm.innerHTML = "Hello<br>everybody";
}

Daniel
 
R

RobB

Tom Szabo said:
Hi,

What I like to know is if it is possible to have a JavaScript writing code
into a DIV section, that is say during load or on a certain event in the
same page it would insert the word "hello"

----------------
......
<script>
function insert_hello(){
?????
}
</script>

<body onload="insert_hello()"....

<div id=div1 name=div1>

</div>
...

-----------------

..so it would display the word "hello" in the area taken up by the <DIV
id=div1 name=div1>
I understand you can change attributes/properties like the value of a text
field but what about this scenario???


TIA,

Tom

Read all about it:

http://www.scottandrew.com/weblog/articles/dom_1
 
J

Julian Turner

Hello

You surely can.

There are two ways to do this.

Firstly use the innerHTML property (supported by IE and Mozilla) which
takes HTML mark up as a string.

document.getElementById("div1").innerHTML="<P>Hello</P>";

Alternatively use DOM methods:-

var eP=document.createElement("P");
var eT=document.createTextNode("Hello");
eP.appendChild(eT);
document.getElementById("div1").appendChild(eP);

Julian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top