SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Threaded View

  1. #1
    SitePoint Addict MrBaseball34's Avatar
    Join Date
    Sep 2002
    Location
    Round Rock, Texas
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [RESOLVED] Problem with Cloning Nodes with Inputs

    In the code below, I have three problems and a question.

    1. When I remove a selection from the listbox, it clears all values. I want to
    keep the values for the ones that are still selected.

    2. Tabbing through the inputs does not work. How do I need to handle this?

    3. I also need it to work for two selects. It was originally written for one
    and I think I've butchered it.

    4. How will the entered data be sent in the postdata? I will be using PHP and need to know so
    I can prepare to iterate the values for input into a database.

    Code:
    <html>
      <head>
        <script>
          var Vname               = new Array();            
          var Hname               = new Array();            
          function clonePitchers(HorV)
          {
            if (HorV = 'V') {
              var sel1             = document.getElementById("sel1V");
              var divPV            = document.getElementById("divPV");
              var divPitchersV     = document.getElementById("divPitchersV");
              var cloneCount       = divPitchersV.childNodes.length;
              var selectedCountV   = 0;
            } else {
              var sel1             = document.getElementById("sel1H");
              var sel1             = document.getElementById("sel1");
              var divPV            = document.getElementById("divPH");
              var divPitchersH     = document.getElementById("divPitchersH");
              var cloneCount       = divPitchersH.childNodes.length;
              var selectedCountH   = 0;
            }
            
            var clone              = new Array();
            
            for(var currentOptionIndex=0; currentOptionIndex<sel1.options.length;currentOptionIndex++)
            {
              if(sel1.options[currentOptionIndex].selected)
              {
                if (HorV = 'V') 
                {
                  document.getElementById("Vpitcher").innerHTML = "";
                  Vname[currentOptionIndex] = sel1.options(currentOptionIndex).text;          
                  selectedCountV++;        
                } else {
                  document.getElementById("Hpitcher").innerHTML = "";
                  Hname[currentOptionIndex] = sel1.options(currentOptionIndex).text;          
                  selectedCountH++;    
                }    
              } else {
                if (HorV = 'V') 
                {
                  Vname[currentOptionIndex] = "";
                } else {
                  Hname[currentOptionIndex] = "";
                }
              }
            }
    
            // This removes ALL nodes. I just want to remove the ones
            // that don't have a value in it's corresponding pitchcount
            // field. Is that possible?        
            for(var i=cloneCount;i>0;i--) {
              if (HorV = 'V') {
                divPitchersV.removeChild(divPitchersV.lastChild);
              } else {
                divPitchersH.removeChild(divPitchersH.lastChild);
              }
            }
          
            if (HorV = 'V') {
              for(var i=0;i<6;i++)
              {
                if (name[i] != "")
                {      
                  document.getElementById("Vpitcher").innerHTML = Vname[i] + "<br/>";
                  document.getElementById("Vpitchcount").setAttribute("id", "Vpitchcount" + i);
                  clone[i] = divPV.cloneNode(true);
                  clone[i].removeAttribute("id");
                  if (i < 5)
                  {
                    clone[i].style.display="block";
                    divPitchersV.appendChild(clone[i]);
                  }
                }
              }
            } else {
              for(var i=0;i<6;i++)
              {
                if (name[i] != "")
                {      
                  document.getElementById("Hpitcher").innerHTML = Hname[i] + "<br/>";
                  document.getElementById("Hpitchcount").setAttribute("id", "Hpitchcount" + i);
                  clone[i] = divPV.cloneNode(true);
                  clone[i].removeAttribute("id");
                  if (i < 5)
                  {
                    clone[i].style.display="block";
                    divPitchersH.appendChild(clone[i]);
                  }
                }
              }
              }
          }
          
          
          function Submit()                    
          {
            var cellvalue;
            var element;
            for(var i=0;i<10;i++)
            {      
              if (Vname[i] != "")
              {
                element = "Vpitchcount" + i;
                cellvalue = document.getElementById(element).getAttribute("value");
                alert(document.getElementById(element).getAttribute("id") + " = " + cellvalue);
              }                  
              if (Hname[i] != "")
              {
                element = "Hpitchcount" + i;
                cellvalue = document.getElementById(element).getAttribute("value");
                alert(document.getElementById(element).getAttribute("id") + " = " + cellvalue);
              }                  
            }
          }
        </script>
      </head>
      <body>
        Visitors's Pitchers:<br />
        <!-- This dropdown will be built from the database of Players based on the visitor's TeamID that
             is stored in the game info. When you enter a game to report, the script eill populate the
             combo for Visitor and Home based on the visitorid and homeid field from the games
             table for the game you want to edit.
        -->
        <select id="sel1V" multiple="true" onchange="clonePitchers('V')">
          <option value="0">34 - Nolan Ryan</option>
          <option value="1">21 - Roger Clemens</option>
          <option value="2">22 - Randy Johnson</option>
          <option value="3">42 - Bob Gibson</option>
          <option value="4"> 2 - David Wells</option>
        </select>
        
        <table height="20px" width="60%" id="divPV" style="display:none">
          <tr height="20px">
            <td width="33%" valign="bottom">Pitcher: <span id="Vpitcher">***</span></td>
            <td width="33%"><span><input type="text" name="Vpitchcount" id="Vpitchcount" size="3">&nbsp;</span></td>
          </tr>
        </table>
        <br />
        Home Team's Pitchers:<br />
        <!-- This dropdown will be built from the database of Players based on the visitor's TeamID that
             is stored in the game info. When you enter a game to report, the script eill populate the
             combo for Visitor and Home based on the visitorid and homeid field from the games
             table for the game you want to edit.
        -->
        <select id="sel1H" multiple="true" onchange="clonePitchers('H')">
          <option value="0">34 - Nolan Ryan</option>
          <option value="1">21 - Roger Clemens</option>
          <option value="2">22 - Randy Johnson</option>
          <option value="3">42 - Bob Gibson</option>
          <option value="4"> 2 - David Wells</option>
        </select>
        
        <table height="20px" width="60%" id="divPH" style="display:none">
          <tr height="20px">
            <td width="33%" valign="bottom">Pitcher: <span id="Hpitcher">***</span></td>
            <td width="33%"><span><input type="text" name="Hpitchcount" id="Hpitchcount" size="3">&nbsp;</span></td>
          </tr>
        </table>
        <form name="main" id="main" action="inputtest.html" method="get">
          <input type="hidden" name="clonecount">
          <span id="divPitchersV"></span>
          <span id="divPitchersH"></span>
          <input type="button" value="Submit" onclick="Submit();">
        </form>  
      </body>
    </html>
    Last edited by MrBaseball34; Nov 15, 2007 at 12:08.
    MrBaseball34
    Hook'Em Horns!


Bookmarks

Posting Permissions

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