Current location: Hot Scripts Forums » General Web Coding » JavaScript » accessing DOM HTML Elements in a dynamically built table.


accessing DOM HTML Elements in a dynamically built table.

Reply
  #1 (permalink)  
Old 02-03-06, 09:44 AM
drdexter33 drdexter33 is offline
New Member
 
Join Date: Sep 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
accessing DOM HTML Elements in a dynamically built table.

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:

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>
**********************************************************
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

Last edited by TwoD; 02-04-06 at 03:41 PM. Reason: Please use [code][/code] wrappers!
Reply With Quote
  #2 (permalink)  
Old 02-03-06, 10:09 AM
ermau's Avatar
ermau ermau is offline
Wannabe Coder
 
Join Date: Aug 2003
Location: Florida, USA
Posts: 240
Thanks: 0
Thanked 0 Times in 0 Posts
For one I'd recommend using lower case element names.

Manipulating the DOM through JS does not produce visible source, at all. To manipulate things you can do something like this:
Code:
var table = document.createElement('table');
table.setAttribute('id', 'myTable');
/* In this code block, document.getElementByID('myTable')
will be exactly the same as table so: */
table.style.width = '100px';
// and
document.getElementByID('myTable').style.width = '100px';
// both do the same thing.
Hope that helps,
Eric
Reply With Quote
  #3 (permalink)  
Old 02-03-06, 02:40 PM
drdexter33 drdexter33 is offline
New Member
 
Join Date: Sep 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks!

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.dubbeldam.com

http://www.microsoft.com/downloads/d...DisplayLang=en

Thanks again...

doug
Reply With Quote
  #4 (permalink)  
Old 02-04-06, 03:43 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
View source doesn't show any changes because it shows you what's in the actual file in the browser cache (Temporary Internet Files id using IE). That file is never manipulated by the script, only what's in the memory cache.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Table in DOM (Add/Remove Rows) codexomega JavaScript 0 08-16-05 11:43 AM
Numbering HTML Table rows lppa2004 PHP 11 07-18-05 01:22 PM
Advice parsing a html table to xml RossC0 JavaScript 3 03-22-05 02:10 PM
get the table elements from other page using a background search script poison~IVY JavaScript 1 03-21-05 10:08 AM
Fixing table cell height - dynamically resize column widths? ijg0 CSS 2 04-19-04 11:34 AM


All times are GMT -5. The time now is 11:03 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.