Firefox, IE, DOM/Javascript Issue

Discussion in 'Web Design & Coding' started by jimi_81, Jul 29, 2006.

  1. jimi_81

    jimi_81 Moderator Political User

    Messages:
    820
    Location:
    Stoney Creek, ON, Canada
    Hey guys..
    I've thrown together a script that only works in IE.. because I use document.elements to sum form values.

    Firefox doesnt like that. Firefox needs document.getElementsbyID
    I have no idea how to implement this...

    Code:
      <script>
      function addscore".$teamid."(obj)
      {
       var total = 0;
       var tmp=0;
       for (i=0; i< enterscore.elements['".$pstat."'].length; i++)
       {
        tmp =Number(enterscore.elements['".$pstat."'][i].value);
        total = Number(total + tmp);
       } 
       enterscore.totgoals".$teamid.".value=total; 
      } 
      </script>
    With input fields:
    Code:
    <Input TYPE='text' NAME='$pstat' value='0' onChange = 'addscore".$teamid."(this)' onblur = 'submitForms".$teamid."(this)' MAXLENGTH='2' SIZE='2'>
    $pstat is a variable...$pstat = "player_stat_goals03[]";

    My question is probably not as simple as.. how do I implement this to work in firefox. I tried changing the NAME to ID, but that pretty much did nothing. When I look at javascript and DOM I get a headache instantly. Is there any OSNN'r around that feels comfortable it?

    thanks for any help!
     
  2. albybum

    albybum Penguin Rancher

    Messages:
    280
    Location:
    Elizabethton, TN
    PHP:
    <?

    $teamid="team1";
    $pstat="player_stat_goals03";

      print 
      
    "<script>
      function addscore"
    .$teamid."(obj)
      {
       var total = 0;
       var tmp=0;
       for (i=0; i< parseInt(obj.value.length); i++)
       {
        tmp =parseInt(obj.value[i]);
        total = parseInt(total + tmp);
       } 
       window.alert(total); // changed for testing
      } 
      </script>
      "
    ;

    print 
    "
    <Input TYPE='text' id='
    $pstat' value='10' onChange = 'addscore".$teamid."(this)' onblur = 'submitForms".$teamid."(this)' MAXLENGTH='2' SIZE='2'>
    "
    ;

    ?>

    Does this do what you want?

    http://albybum.net/temp/test.php
     
    jimi_81 likes this.
  3. jimi_81

    jimi_81 Moderator Political User

    Messages:
    820
    Location:
    Stoney Creek, ON, Canada
    I'm going to try the idea out, but man, because you put in the effort, i gotta rep you. thanks for taking the time man!
     
  4. albybum

    albybum Penguin Rancher

    Messages:
    280
    Location:
    Elizabethton, TN
    Looking back over the original code, I don't think my answer did what you wanted.

    I think this does. Full page example.
    http://www.albybum.net/temp/test.php

    PHP:
    <html>
    <head>
    <?
    $teamid
    ="team1";
    $pstat="player_stat_goals03";
    print 
    "<script>
    function addscore"
    .$teamid."(obj)
    {
    var total = 0;
    var tmp=0;
    var es='';
    es=document.getElementsByName('
    $pstat');
     
    for (i=0; i< parseInt(es.length); i++)
    {
    tmp =parseInt(es[i].value);
    total = parseInt(total + tmp); 
    }
    enterscore.totgoals"
    .$teamid.".value=total; 

    </script>
    "
    ;
    ?>
    </head>
    <body>
    <form name="enterscore">
    <?
    print 
    "
    <input type='text' name='
    $pstat' value='10' onChange = 'addscore".$teamid."(this)' onblur = 'submitForms".$teamid."(this)' MAXLENGTH='2' SIZE='2'>
    <input type='text' name='
    $pstat' value='10' onChange = 'addscore".$teamid."(this)' onblur = 'submitForms".$teamid."(this)' MAXLENGTH='2' SIZE='2'>
    <br /><br />
    <input type='text' name='totgoals
    $teamid' value=''>
    "
    ;
    ?>
    </form>
    </body>
    </html>
     
    Geffy likes this.
  5. jimi_81

    jimi_81 Moderator Political User

    Messages:
    820
    Location:
    Stoney Creek, ON, Canada
    that seems to do exactly what i need. i will plug that into our code and let ya know for sure.

    thanks alot, i really really appreciate it.
    i've given you all the reps i can for the time being, we need some rep loving from the heavy hitter guys. kynchief... hehe