0

Hello I need to have fields be added and taken away with plus and minus buttons. Whenever I add a field it works fine. When I enter something into a field then add another field, the previous field resets. Also, whenever I click the minus button it removes all of the fields. I want it to take one away at a time without resetting the other fields and also I keep getting a Not a Number (NaN) error whenever I click the minus button. Any help is greatly appreciated. I'll show my code below.

<input name="+" type="button" id="+" onclick="addInput()" value="+" label="+" />
<input name="-" type="button" id="-" onclick="subtractInput()" value="-" label="-" />

<span id="response"></span>
<script>
    var countBox = 1;
    var boxName = 0;

    function addInput() {
        var boxName = "#";
        document.getElementById('response').innerHTML += '<br/><input type="text" id="' + boxName + '" value="' + boxName + '" "  /><br/>';
        countBox += 1;
    }

    function subtractInput() {
        var boxName = "#";
        document.getElementById('response').innerHTML -= '<br/><input type="text" id="' + boxName + '" value="' + boxName + '" "  /><br/>';
        countBox -= 1;
    }
</script>
Blundering Philosopher
  • 6,245
  • 2
  • 43
  • 59
GraphicsRPS
  • 15
  • 1
  • 5

4 Answers4

0

I don't think you can remove an element with normal JS. So, here is some jQuery.

var countBox = 1;
var boxName = 0;

function addInput() {
    var boxName = "#";
    document.getElementById('response').innerHTML += '<br/><input type="text" id="' +     countBox + '" value="' + boxName + '" "  /><br/>';
    countBox += 1;
}

function subtractInput() {
    var boxName = "#";
    $(function(){
    $('#' + countBox).remove();
    });
    countBox -= 1;
}

I tested this myself, and it does work. Problem is, it keeps adding to the position where the last one was removed, not back where it should be starting. Here's the jQuery link for you:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

  • Oh, and append has the same issue with adding past where it should be. – DaMuffiner Jan 30 '14 at 15:30
  • When I tried the code you provided, it just adds now. Nothing happens when I click the minus button. It removed them before, just gave an NaN next to the button. Still have the problem of when adding a field it resets the previous one if I enter a number or anything into it. I want to prevent that while still being able to add a field. – GraphicsRPS Jan 30 '14 at 15:38
  • Odd. When I tried your code it gave me the NaN, but mine still works. Perhaps try `$(function(){ $('#response').append('

    ')});`
    – DaMuffiner Jan 30 '14 at 17:01
  • Tested again, still works. I did just notice I have to hit the '-' button twice to get it to subtract one. – DaMuffiner Jan 30 '14 at 17:08
  • Btw, where should I input the code you just provided? I tried placing it in and nothing happens then. Both buttons do nothing? Sorry if this is frustrating. – GraphicsRPS Jan 30 '14 at 17:20
0

Based on a similar question asked previously, it looks like this can't be done using javascript, but it can be done with jQuery's function append().

Community
  • 1
  • 1
socki03
  • 344
  • 1
  • 8
0

Try this out!

I wrapped your code within a div [and removed the 'response' span]

<div id="container">
    <input name="+" type="button" id="+" onclick="addInput()" value="+" label="+" />
    <input name="-" type="button" id="-" onclick="subtractInput()" value="-" label="-" />
</div>

Then changed up your javascript as shown here:

var countBox = 1;

function addInput() {
    var breakNode = document.createElement("br");
    breakNode.className = countBox.toString();

    var input = document.createElement("input");
    input.type = "text";
    input.className = countBox.toString();
    input.value = "#" + countBox.toString();

    var container = document.getElementById('container');
    container.appendChild(breakNode);
    container.appendChild(input);

    countBox += 1;
}

function subtractInput() {
    countBox -= 1;

    // There will be 2 elements: 1 input, 1 br.
    var elements = document.getElementsByClassName(countBox.toString());
    var input = elements[1];
    var br = elements[0];

    input.parentNode.removeChild(input);
    br.parentNode.removeChild(br);
}

Try it with this Fiddle

Let me know if you have problems with it!

EDIT: I made a cleaner version you can now check out by the way :)

References:

  1. Create Input Element Dynamically
  2. Remove Child Node in HTML

EDIT 2: Here's the solution to your new question:

First, I added a 1 to the name ECPartNumber of the first input, and changed its id to 1:

<input name="ECPartNumber1" type="text" id="1" value="#" size="10" maxlength="10">

The reason I changed the id to 1 is because the following inputs have their ids counting up from 1, so I thought this would look nicer.

Next, I declared var countBox = 1; at the top of the javascript file so that the FrontPage_Form1_Validator function would be able to use it.

In the addInput function, I added a line to create the new names for each input:

input.name = "ECPartNumber" + countBox.toString();

In the subtractInput function, I added an if statement to make sure you couldn't delete the original ECPartNumber input:

if (countBox >= 2) {
    ...
}

And finally, in the FrontPage_Form1_Validator function I made a for loop that runs off the names of each ECPartNumber to check if they are "#" or more:

for (var i = 1; i <= countBox; i++) {
    var partNum = "ECPartNumber" + i.toString();
    if (theForm[partNum].value == "#") {
        alert("Please enter a value for the \"EC Part Number\" field. (slot "+i+")");
        theForm[partNum].focus();
        return (false);
    }
}

Updated Fiddle

Community
  • 1
  • 1
Blundering Philosopher
  • 6,245
  • 2
  • 43
  • 59
  • Thank You so much. This is exactly what I was looking for. If I have any further questions on this I will direct it to this question. Thank You again! – GraphicsRPS Jan 30 '14 at 18:57
  • Ok I was able to implement the adding and subtracting into the form I needed it for. Now I need to figure out how to get it to show up in the output. When I submit I need to have the fields added show up in the email. Also I need to figure out how to have the fields added become required once added and not required once taken away. I have the script setup for every other field and you can check it out to see if you can help me. Thank You so much. Also to anyone else helping Thank You too. [Fiddle](http://jsfiddle.net/GraphicsRPS/r2tDQ/2/) – GraphicsRPS Jan 31 '14 at 16:12
  • Groovy, I can definitely help with that! It looks like you used my old answer in your fiddle - do you mind if I change those functions to my most recent version? I think it would be a tad simpler. – Blundering Philosopher Jan 31 '14 at 17:01
  • Thank You this is perfect, I will just need to adjust a few things and it should work perfectly. This is great. If I have any further questions I will be sure to post them here. You are amazing. Thank You. – GraphicsRPS Jan 31 '14 at 17:41
  • How do I accept the answer, I want to help you out anyway possible. I can't vote anything since my rep is below 15. – GraphicsRPS Jan 31 '14 at 19:14
0

First of all, new browsers don't let you copy the changes in html made by users, that is why the "previous field resets"

Now a way to it, using just javascript is:

<script>
    var countBox = 0;
    var boxName = "SomeId";

    var addInput = function(){
        alert(boxName+countBox);
        var br = document.createElement("br");
        var input = document.createElement("input");
        input.id=boxName+countBox;
        input.value=boxName;
        document.getElementById("response").appendChild(input);
        document.getElementById("response").appendChild(br);
        countBox++;
    }

    var subtractInput = function(){
        countBox--;
        alert(boxName+countBox);
        document.getElementById("response").removeChild(document.getElementById(boxName+(countBox)));
        document.getElementById("response").removeChild(document.getElementById('response').getElementsByTagName('br')[countBox]);
    }
</script>

You can find a working example here

Paraíso
  • 384
  • 2
  • 8