Need Help!!! Javascript DOM

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • traineeirishprogrammer
    New Member
    • Jul 2007
    • 24

    Need Help!!! Javascript DOM

    I am currently learning javascript and DOM. Basically I am currently writing a function to add rows to a table and I am trying to do it using DOM. Each row is made up of 4 cells. Here is the code!!!

    [CODE=javascript]function addRow()
    {
    var tbl=document.ge tElementById('g ame');
    var lastRow = tbl.rows.length ;
    var row = tbl.insertRow(l astRow);

    var cell_1 = row.insertCell( 0);
    var cell_2= row.insertCell( 1);
    var cell_3 = row.insertCell( 2);
    var cell_4 = row.insertCell( 3);


    cell_1.innerHTM L='<div align="center"> <input type="button" onclick="delete Row(this)" value="Delete" ></div>';

    var team_1 = document.create Element("select ");

    team_1.setAttri bute('name','te am_A');

    alert(team_1.na me);
    team_1.options[0] = new Option("Ballina ",1);
    team_1.options[1] = new Option("Silverm ines",2);
    team_1.options[2] = new Option("Kilruan e",3);
    team_1.options[3] = new Option("Nenagh" ,4);
    team_1.options[4] = new Option("Borrisa kane",5);
    team_1.options[5] = new Option("Potroe" ,6);
    team_1.options[6] = new Option("Burgess ",7);
    team_1.options[7] = new Option("Kiladan gan",8);
    team_1.options[8] = new Option("Lorrah" ,9);
    team_1.options[9] = new Option("Roscrea ",10);
    team_1.options[10] = new Option("TempleD erry",11);
    team_1.options[11] = new Option("Shannon Rovers",12);

    cell_2.appendCh ild(team_1);



    var team_2 = document.create Element('select ');


    team_2.name = 'team_B';



    team_2.options[0] = new Option("Ballina ",1);
    team_2.options[1] = new Option("Silverm ines",2);
    team_2.options[2] = new Option("Kilruan e",3);
    team_2.options[3] = new Option("Nenagh" ,4);
    team_2.options[4] = new Option("Borrisa kane",5);
    team_2.options[5] = new Option("Potroe" ,6);
    team_2.options[6] = new Option("Burgess ",7);
    team_2.options[7] = new Option("Kiladan gan",8);
    team_2.options[8] = new Option("Lorrah" ,9);
    team_2.options[9] = new Option("Roscrea ",10);
    team_2.options[10] = new Option("TempleD errteam_2",11);
    team_2.options[11] = new Option("Shannon Rovers",12);

    cell_3.appendCh ild(team_2);



    var mydate= new Date()
    var theyear=mydate. getFullYear()
    var themonth=mydate .getMonth()+1
    var thetoday=mydate .getDate()

    var dateinput = document.create Element("input" );
    dateinput.type = 'text';
    dateinput.id = 'no'+lastRow;
    dateinput.value =thetoday+'-'+themonth+'-'+theyear;
    dateinput.size= '25';
    cell_4.appendCh ild(dateinput);

    var getdate = document.create Element('a');
    getdate.href= "javascript:New Cal('no"+lastRo w+"','ddmmyyyy' )";
    getdate.innerHT ML= "<img src='../../calender/cal.gif' width='16' height='16' border='0' alt='Pick a date'>";
    cell_4.appendCh ild(getdate);

    }[/CODE]

    The CODE is not entirely finished but my problem is when I try to access the select list "team_A" either through getElementsByNa me or "team_B" . It cant find it. Can anyone tell me where I am going wrong. Thanks
    Last edited by gits; Jul 20 '07, 05:20 AM. Reason: added CODE-tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    are you using IE? IE don't let you set the name of a form-element properly. have a look at:



    other browsers do well ... only the ie-jscript makes a problem here.

    kind regards

    Comment

    • traineeirishprogrammer
      New Member
      • Jul 2007
      • 24

      #3
      Thanks I tried

      team_1 = document.create Element("<selec t name='team_A'></select>");

      instead of

      team_1 = document.create Element("select ");
      team_1.name = " team_A";

      However at the end of the function just for testing purposes I used

      alert(document. getElementsByNa me('team_A').na me);

      Just to see the result.

      and the alert came up with te result undefined

      Do you know where the problem is in this script.

      Thanks.

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        thats because

        document.getEle mentsByName('te am_A').name

        is plural so you gotta tell it which team_a to get
        document.getEle mentsByName('te am_A')[0].name

        Comment

        • traineeirishprogrammer
          New Member
          • Jul 2007
          • 24

          #5
          Thanks for your help. I will ask more questions as I progress through my project.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            Originally posted by traineeirishpro grammer
            Thanks for your help. I will ask more questions as I progress through my project.
            no problem ;) you and your questions are always welcome ... post back anytime ...

            kind regards

            Comment

            Working...