Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2005
    Location
    Wash DC
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    trouble with getElementsByTagName

    hello Team,
    I was trying to use the getElementsByTagName to list all the tables and forms in a document....however it appears that the result of the table elements is just "undefined". What am I doing wrong?
    thx!!

    =====
    htlm/js code
    =======
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>JS Forms Play</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript1.2" type="text/JavaScript">
    <!--
    function listObjects() {
    var myTables = document.getElementsByTagName("TABLE");
    var myForms = document.getElementsByTagName("FORM");
    var myTableslen = myTables.length;
    var myFormslen = myForms.length;
    var name;
    document.writeln("Num of tables found: "+myTableslen+"<br>");
    document.writeln("Num of forms found: "+myFormslen+"<br>");
    document.writeln("Table List:<br>");
    for (x=0; x<myTableslen; x++) {
    document.writeln("TableName=>"+myTables[x]+"<br>");
    }
    document.writeln("Form List:<br>");
    for (x=0; x<myFormslen; x++) {
    document.writeln("FormName=>"+myForms[x]+"<br>");
    }

    }
    function setAll() {

    }
    // -->
    </script>
    </head>
    <body>
    <table width="300" border="1" cellspacing="0" cellpadding="0" ID=T1>
    <tr>
    <th>Table#1 Header</th>
    </tr>
    <td>example table line</td></tr>
    </TABLE>
    <br>
    <table width="300" border="1" cellspacing="0" cellpadding="0" ID=T2>
    <tr>
    <th>Table#2 Header</th>
    </tr>
    <td>example table line2</td></tr>
    </TABLE>
    <h1>Form Test</h1>
    <FORM action="" method="GET" name="myForm" id="myForm" >
    My Name: <input name="frm_name" type="text" id="frm_name" value="Example Name" size="40" maxlength="40">
    <br>
    checkbox1: <input type=checkbox name="cb1" id="cb1" value=11><br>
    checkbox2: <input type=checkbox name="cb2" id="cb2" value=22><br>
    checkbox3: <input type=checkbox name="cb3" id="cb3" value=33><br>
    <input type="BUTTON" name="btnall" id="btnall" value="Check All"
    onClick="setAll();"><br>
    <input type="BUTTON" name="btnobjects" id="btnobjects" value="List Objects"
    onClick="listObjects();"><br>
    <input type="SUBMIT" name="frmSubmit" id="frmSubmit" value="Submit">
    <input type="RESET" name="frmReset" id="frmReset" value="Reset">
    </FORM>
    </body>
    </html>

    ======
    Results when I click on List Objects button:
    ================

    Num of tables found: 2
    Num of forms found: 1
    Table List:
    TableName=>undefined
    TableName=>undefined
    Form List:
    FormName=>undefined

  • #2
    New Coder
    Join Date
    Aug 2004
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Checkout this code...

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>JS Forms Play</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript1.2" type="text/JavaScript">
    <!--
    function listObjects() {
    var myTables = document.getElementsByTagName("TABLE");
    var myForms = document.getElementsByTagName("FORM");
    var myTableslen = myTables.length;
    var myFormslen = myForms.length;
    var name;
    alert("Num of tables found: "+myTableslen);
    alert("Num of forms found: "+myFormslen);
    alert("Table List :");
    for (x=0; x<myTableslen; x++) {
    alert("TableName=>"+myTables[x].id);
    }
    alert("Form List :");
    for (x=0; x<myFormslen; x++) {
    alert("FormName=>"+myForms[x].id);
    }
    
    }
    function setAll() {
    
    }
    // -->
    </script>
    </head>
    <body>
    <table width="300" border="1" cellspacing="0" cellpadding="0" id="T1">
    <tr>
    <th>Table#1 Header</th>
    </tr>
    <td>example table line</td></tr>
    </TABLE>
    <br>
    <table width="300" border="1" cellspacing="0" cellpadding="0" id="T2">
    <tr>
    <th>Table#2 Header</th>
    </tr>
    <td>example table line2</td></tr>
    </TABLE>
    <h1>Form Test</h1>
    <FORM action="" method="GET" name="myForm" id="myForm" >
    My Name: <input name="frm_name" type="text" id="frm_name" value="Example Name" size="40" maxlength="40">
    <br>
    checkbox1: <input type=checkbox name="cb1" id="cb1" value=11><br>
    checkbox2: <input type=checkbox name="cb2" id="cb2" value=22><br>
    checkbox3: <input type=checkbox name="cb3" id="cb3" value=33><br>
    <input type="BUTTON" name="btnall" id="btnall" value="Check All"
    onClick="setAll();"><br>
    <input type="BUTTON" name="btnobjects" id="btnobjects" value="List Objects"
    onClick="listObjects();"><br>
    <input type="SUBMIT" name="frmSubmit" id="frmSubmit" value="Submit">
    <input type="RESET" name="frmReset" id="frmReset" value="Reset">
    </FORM> 
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Feb 2005
    Location
    Wash DC
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cool!!! I see now why my document.writeln did not work and I kept getting undefined and object does not exist. When i used the writeln it overwrote my current window destroying my previous objects such as tables for the form. I have include a window.open now:
    var auxwin = window.open("","auxwindow");

    I updated my writeln to reference the new window for writing to:
    auxwin.document.writeln("TableName=>"+name+"<br>");

    Now it's working.....THANKS!!
    Styx


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •