Click to See Complete Forum and Search --> : Accessing DOM HTML Elements in a dynamically built table.


drdexter33
02-03-2006, 09:50 AM
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

konithomimo
02-03-2006, 09:56 AM
The source is what code the page has on your server, meaning when it first loads, before any code runs. That means that you will only be able to see what code you write, and none that is created through scripting.

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

That is not true. If you create html code through scripting then you can still manipulate that code. There are only a few things that you cannot manipulate (such as javascript that is created through scripting).

drdexter33
02-03-2006, 02:38 PM
my javascript skills are a little wanting...

learned some new things today..

Mainly: Right-click->view source doesn't show dynamically some generated HTML code...

Use a DOM explorer to see source code being generated dynamically...

www.dubbledam.com

http://www.microsoft.com/downloads/details.aspx?FamilyID=e59c3964-672d-4511-bb3e-2d5e1db91038&DisplayLang=en

Thanks again...

doug