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

accessing DOM HTML Elements in a dynamically built table.

Status
Not open for further replies.

drdexter33

Programmer
Jun 11, 2003
17
US
I have a question:

Say I am building an HTML table, and as an example, I'll use this sample taken from the Mozilla Developer Center website:

START CODE

***********************************************************
<head>
<title>Sample code - Traversing an HTML Table with JavaScript and DOM Interfaces</title>
<script>
function start() {
// get the reference for the body
var mybody=document.getElementsByTagName("body").item(0);
// creates an element whose tag name is TABLE
mytable = document.createElement("TABLE");
// creates an element whose tag name is TBODY
mytablebody = document.createElement("TBODY");
// creating all cells
for(j=0;j<2;j++) {
// creates an element whose tag name is TR
mycurrent_row=document.createElement("TR");
for(i=0;i<2;i++) {
// creates an element whose tag name is TD
mycurrent_cell=document.createElement("TD");
// creates a Text Node
currenttext=document.createTextNode("cell is row "+j+", column "+i);
// appends the Text Node we created into the cell TD
mycurrent_cell.appendChild(currenttext);
// appends the cell TD into the row TR
mycurrent_row.appendChild(mycurrent_cell);
}
// appends the row TR into TBODY
mytablebody.appendChild(mycurrent_row);
}
// appends TBODY into TABLE
mytable.appendChild(mytablebody);
// appends TABLE into BODY
mybody.appendChild(mytable);
// sets the border attribute of mytable to 2;
mytable.setAttribute("border","2");
}
</script>
</head>
<body onload="start()">
</body>
</html>



***********************************************************
END CODE

Problem:
Why is there no HTML when I view the source?

And since (strangely) there is no HTML, I cannot write any JavaScript/DHTML functionality to manipulate what I just created...
Why is this true?

Thanks.

Blessings.

Doug Dexter
 
Viewing the source normally shows you only the source delivered to the page, not what you have dynamically modified / created - this is perfectly normal behaviour.

You should, however, have no problems using DOM methods to modify the code once written to the page - if you are, then I'd say your code has a problem.

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Dan.

Thanks for your reply.

Well, shouldn't the HTML code, written by the start() function, be visible when I right click(in IE6)and select View Source?

Thanks again!

Doug
 
type this into the browser's address bar to see the code as the browser has interpreted it - it's very useful for debugging dynamically created code:
Code:
javascript:void(window.open("javascript:document.open(\"text/plain\");document.write(opener.document.body.parentNode.outerHTML)"))

[sub](sorry for the stretched post)[/sub]

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top