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
    Mar 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Are table elements not required in HTML tree ?

    Code:
    <script type="text/javascript">
    function validate(form) {
    // Checking if at least one period button is selected. Or not.
    if (!document.form1.sex[0].checked && !document.form1.sex[1].checked){
    
    
    alert("Please Select Sex"); return false;}
    
    var total=""
    for(var i=0; i < document.form1.scripts.length; i++){
    if(document.form1.scripts[i].checked)
    total +=document.form1.scripts[i].value + "\n"
    }
    if(total=="")
    alert("select scripts") else
    alert (total)
    
    return false;
    } </script>
    
    <table border='0' width='50%' cellspacing='0' cellpadding='0' ><form name=form1 method=post action=action_page.php onsubmit='return validate(this)'><input type=hidden name=todo value=post>
    
    <tr bgcolor='#ffffff'><td align=center ><font face='verdana' size='2'><b>Sex</b><input type=radio name=sex value='male'>Male </font><input type=radio name=sex value='female'><font face='verdana' size='2'>Female</font></td></tr>
    
    <tr><td align=center bgcolor='#f1f1f1'><font face='verdana' size='2'><b>Scripts You know</b><input type=checkbox name=scripts value='JavaScript'>JavaScript <input type=checkbox name=scripts value='PHP'>PHP <input type=checkbox name=scripts value='HTML'>HTML </td></tr>
    
    <tr bgcolor='#ffffff'><td align=center ><input type=submit value=Submit> <input type=reset value=Reset></td></tr>
    
    </table></form>
    In this code a checkbox is accessed as "document.form1.scripts[i].checked"
    If we see the tree view of this document, it is "table/form1/tr/td/scripts[i].checked"

    SO are we not required to add table elements in the path to the checkbox ? I guess its not required. But why ? Because cell, row and table are the parent elements in HTML tree, then why are they not required ?

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    945
    Thanks
    0
    Thanked 129 Times in 128 Posts
    The call looks for the element with name form1, and inside that the first element by name script.
    Just like document.getElementById() finds the element whereever it is, refering to a form name does too.
    document.form1.script[i] is more or less interpreted as document.getFormByName('form1')getElementsByName('script')[i]

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    When you use the forms collection it only includes form elements - input, select, textarea

    Similarly if you use the table collection it includes all the table elements - caption, thead, tfoot, tbody, rows and cells.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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