1

I'm having a problem with this form I'm 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">

    <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 Answer 1

1

The Following addition should work:

var $clonedTextBox = $textBox.clone();
$($clonedTextBox).val('');
Sign up to request clarification or add additional context in comments.

1 Comment

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.