views:

139

answers:

1

Hi, Im having problem with this form im working on. Whenever I add, or refresh the page, the values are still there. I believe this is because the clone method copies the value attribute from the textBox. Is there any way I can get rid of them when i add another textBox.

<html>
  <head>
    <title>JQuery Example</title>
    <script type="text/javascript" src="jquery-1.4.js"></script>
    <script type="text/javascript">


      function removeTextBox()
      {
          var childCount = $('p').size() //keep track of paragraph childnodes

          //this is because there should always be 2 p be tags the user shouldn't remove the first one
           if(childCount != 2)   
          {
            var $textBox = $('#textBox')
            $textBox.detach()

          }

      }


      function addTextBox()
      {

        var $textBox = $('#textBox')
        var $clonedTextBox = $textBox.clone()

        //document.getElementById('textBox').setAttribute('value', "")

        $textBox.after($clonedTextBox)



      }
    </script>
  </head>
  <body>
    <form id =
          method="POST"
          action="http://cs.harding.edu/gfoust/cgi-bin/show"&gt;

    <p id= "textBox">
        Email:
        <input type = "text" name="email" />
        <input type ="button" value ="X" onclick = "removeTextBox()"/>
    </p>

      <p>
        <a href="javascript:addTextBox()">Add another email</a>
      </p>
      <input type="submit" value="submit"/>
    </form>
  </body>
</html>
+1  A: 

The Following addition should work:

var $clonedTextBox = $textBox.clone();
$($clonedTextBox).val('');
Kyle Butt
nope it doesn't work
ProxyProtocol
the var name in the second line of code should be $clonedTextBox, although I think beginning local vars in JS with a $ is a bad practice and especially confusing when used alongside jQuery...
floyd