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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write html code to div from javascript?

Status
Not open for further replies.

404notfound

Technical User
Jun 26, 2006
7
US
I have this nice little js script that can write text to a div:
-----------------------------------------
<script type="text/javascript">
//<![CDATA[
onload=function()
{
var txt=document.getElementById("myDiv")
txt.innerHTML="Some Content";
}
//]]>
</script>

<body>
<div id="myDiv"></div>
</body>
-----------------------------------------

I suppose I'm being greedy but I was wondering if there is any way this or something like this would work with html content. Where you could enter html code where "Some Content" is and have it display in the div? Thanks!
 
I guess i was thinking more like:

//<![CDATA[
onload=function()
{
var txt=document.getElementById("mydiv")
txt.innerHTML="<table width="100" border="0" cellpadding="0" cellspacing="0" bgcolor="#0000FF">
<tr><td>
CONTENT!
</td></tr>
</table>";
}
//]]>

which dosn't quite work...
 
Hi

You have quoting problem :
Code:
  txt.innerHTML=[red]'[/red]<table width="100" border="0" cellpadding="0" cellspacing="0" bgcolor="#0000FF">
<tr><td>
CONTENT!
</td></tr>
</table>[red]'[/red];

[gray]// or[/gray]

  txt.innerHTML="<table width=[red]\[/red]"100[red]\[/red]" border=[red]\[/red]"0[red]\[/red]" cellpadding=[red]\[/red]"0[red]\[/red]" cellspacing=[red]\[/red]"0[red]\[/red]" bgcolor=[red]\[/red]"#0000FF[red]\[/red]">
<tr><td>
CONTENT!
</td></tr>
</table>";

Feherke.
 
? - in conclusive...
I think the problem is because of the forward slashes in the closing table tags... among other things.

</td></tr>
</table>
 
Hi

Oops. I quickly picked the picked evident quoting problem and did not looked forward for others.

You can not wrap the [tt]String[/tt]s in JavaScript like that.
Code:
  txt.innerHTML='<table width="100" border="0" cellpadding="0" cellspacing="0" bgcolor="#0000FF">[red]\[/red]
<tr><td>[red]\[/red]
CONTENT![red]\[/red]
</td></tr>[red]\[/red]
</table>';

[gray]// or[/gray]

  txt.innerHTML='<table width="100" border="0" cellpadding="0" cellspacing="0" bgcolor="#0000FF">[red]'+[/red]
[red]'[/red]<tr><td>[red]'+[/red]
[red]'[/red]CONTENT![red]'+[/red]
[red]'[/red]</td></tr>[red]'+[/red]
[red]'[/red]</table>';

Feherke.
 
Oooh! That's Sweet! Thanks.
And I think I can actually apply it.
Is one method better than the other?
The backslashes seem slightly simpler.
 
Hi

Well, the web pages can be viewed on various systems, which could use different end-of-line marks. And there is a chance that escaping it with backslash ( \ ) will not work everywhere. Better go with the uglier [tt]String[/tt] concatenation.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top