0

I have this existing code which I had some help on: http://jsfiddle.net/975VQ/11/

The issue with this code is I also want to allow them to remove the original field. I cannot remove the original text field and still add another.

Could someone please show me how I could approach this problem instead? I would assume it would be by using javascript to add the first entry, and then using javascript to add in the markup each time rather than cloning it.

var MaxInputs       = 8; //maximum input boxes allowed
var InputsWrapper   = $("#InputsWrapper"); //Input boxes wrapper ID
var AddButton       = $("#addfield"); //Add button ID

var x = InputsWrapper.length; //initlal text box count
var FieldCount=1; //to keep track of text box added

$(AddButton).click(function (e)  //on add input button click
{
        if(x <= MaxInputs) //max input box allowed
        {
            FieldCount++; //text box added increment
            //add input box
            $(InputsWrapper).append('<div><input type="text" name="mytext[]" id="field_'+ FieldCount +'" value="Text '+ FieldCount +'"/><button class="removeclass">Delete</button></div>');
            x++; //text box increment
        }
return false;
});

$("body").on("click",".removeclass", function(e){ //user click on remove text
        if( x > 1 ) {
                $(this).parent('div').remove(); //remove text box
                x--; //decrement textbox
        }
return false;
}) 

Any help or input on this would be appreicated.

1
  • jsfiddle.net/975VQ/15 check if this is what u need. Commented Apr 25, 2014 at 10:29

5 Answers 5

2

You are deleting the field with the id=InputsWrapper on the original field, that is the div where you are ataching the news fields.

You need to put the original field in other div.

<div id="InputsWrapper">
    <div>
         <input value="Why do you want this">
         <select>
             <option value="single">Single line reply</option>
             <option value="multi">Paragraph reply</option>
         </select>
         <button class="removeclass">Delete</button>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Then I get this, which seems to need one remaining: jsfiddle.net/spadez/975VQ/29
If you don't need one remaining delete the if sentence "if( x >= 1 )"
1

try this fiddle, this is what you need

http://jsfiddle.net/975VQ/20/

HTML:

<h1>Questions</h1>
<p>Add additional questions. You can select how long you would like the reply to be.</p>
<div>
<input value="Why do you want this">
<select>
    <option value="single">Single line reply</option>
    <option value="multi">Paragraph reply</option>
</select>

<button class="removeclass">Delete</button>
</div>
    <div id="InputsWrapper"></div>
<br />
<button id="addfield">Add question</button>
<br />

JS:

var MaxInputs       = 8; //maximum input boxes allowed
var InputsWrapper   = $("#InputsWrapper"); //Input boxes wrapper ID
var AddButton       = $("#addfield"); //Add button ID

var x = InputsWrapper.length; //initlal text box count
var FieldCount=1; //to keep track of text box added

$(AddButton).click(function (e)  //on add input button click
{
        if(x <= MaxInputs) //max input box allowed
        {
            FieldCount++; //text box added increment
            //add input box
            $('<div><input type="text" name="mytext[]" id="field_'+ FieldCount +'" value="Text '+ FieldCount +'"/><button class="removeclass">Delete</button></div>').insertBefore(InputsWrapper);
            x++; //text box increment
        }
return false;
});

$("body").on("click",".removeclass", function(e){ //user click on remove text
    console.log(x);
        if( x > 1 ) {
            console.log(x);
                $(this).parent('div').remove(); //remove text box
                x--; //decrement textbox
        }
return false;
})

4 Comments

You cant delete the first entry, it is the same problem as my posted code
are you wanting to be able to delete the first question so there are none left? or not allow the first one to be deleted?
I want to be able to have no questions, but by default start with one
if thats the case you need to take the if(x>1) statement out of the click function
1

check this jsfiddle

 <h1>Questions</h1>
 <p>Add additional questions. You can select how long you would like the reply to be.</p>
 <div id="InputsWrapper">
 <input value="Why do you want this">
 <select>
   <option value="single">Single line reply</option>
   <option value="multi">Paragraph reply</option>
 </select>
 <button class="removeclass">Delete</button>
 </div>
 <br />
 <button id="addfield">Add question</button>
 <br />

javascript

  var MaxInputs       = 8; //maximum input boxes allowed
  var InputsWrapper   = $("#InputsWrapper"); //Input boxes wrapper ID
  var AddButton       = $("#addfield"); //Add button ID

  var x = InputsWrapper.length; //initlal text box count
  var FieldCount=1; //to keep track of text box added

  $(AddButton).click(function (e)  //on add input button click
  {
    if(x <= MaxInputs) //max input box allowed
    {
        FieldCount++; //text box added increment
        //add input box
        $(InputsWrapper).append('<div><input type="text" name="mytext[]" id="field_'+ FieldCount +'" value="Text '+ FieldCount +'"/><button class="removeclass">Delete</button></div>');
        x++; //text box increment
    }
     return false;
    });

    $("body").on("click",".removeclass", function(e){ //user click on remove text
     if( x >= 1  ) {
            $(this).parent('div').remove(); //remove text box
            x--; //decrement textbox
     }
     return false;
    }) 

3 Comments

Hi, that is great, and closer, but when you delete all of them it stops you being able to add a new one
here is updated fiddle jsfiddle.net/975VQ/24 .. check if this solves your problem
Now when you hit delete it deletes all the fields
1

this jsfiddle should be what you want if I understood well,

HTML:

<h1>Questions</h1>
<p>Add additional questions. You can select how long you would like the reply to be.</p>
<div id="InputsWrapper">
<div>
<input value="Why do you want this">
<select>
<option value="single">Single line reply</option>
<option value="multi">Paragraph reply</option>
</select>
<button class="removeclass">Delete</button>
</div>
</div>
<br />
<button id="addfield">Add question</button>
<br />

Javascript:

var MaxInputs       = 8; //maximum input boxes allowed
var InputsWrapper   = $("#InputsWrapper"); //Input boxes wrapper ID
var AddButton       = $("#addfield"); //Add button ID

var x = InputsWrapper.length; //initlal text box count
var FieldCount=1; //to keep track of text box added

$(AddButton).click(function (e)  //on add input button click
{
    if(x <= MaxInputs) //max input box allowed
    {
        FieldCount++; //text box added increment
        //add input box
        $(InputsWrapper).append('<div><input type="text" name="mytext[]" id="field_'+ FieldCount +'" value="Text '+ FieldCount +'"/><button class="removeclass">Delete</button></div>');
        x++; //text box increment
    }
    if(FieldCount == 2 && x > 1)
    {
        $("#InputsWrapper div").first().remove();
    }
return false;
});

$("body").on("click",".removeclass", function(e){ //user click on remove text
    if( x >= 1 ) {
            $(this).parent('div').remove(); //remove text box
            x--; //decrement textbox
    }
return false;
}) 

1 Comment

Again this simply doesnt work, you cant delete the first entry by hitting delete
1

Try This

HTML

        <h1>Questions</h1>
    <p>Add additional questions. You can select how long you would like the reply to be.</p>
    <div class="que">
    <div class="InputsWrapper">
    <input value="Why do you want this">
    <select>
        <option value="single">Single line reply</option>
        <option value="multi">Paragraph reply</option>
    </select>
    <button class="removeclass">Delete</button>
        </div>
        </div>
    <br />
    <button id="addfield">Add question</button>
    <br />

Javascript

    var MaxInputs       = 8; //maximum input boxes allowed
var AddButton       = $("#addfield"); //Add button ID

var x = $(".InputsWrapper").length; //initlal text box count
var FieldCount=1; //to keep track of text box added

$(AddButton).click(function (e)  //on add input button click
{
        if(x <= MaxInputs) //max input box allowed
        {
            FieldCount++; //text box added increment
            //add input box
            $(".que").append('<div class="InputsWrapper"><input type="text" name="mytext[]" id="field_'+ FieldCount +'" value="Text '+ FieldCount +'"/><button class="removeclass">Delete</button></div>');
            x++; //text box increment
        }
return false;
});

$("body").on("click",".removeclass", function(e){ //user click on remove text   

                $(this).parent('div').remove(); //remove text box
                x--; //decrement textbox
return false;
}) 

DEMO

2 Comments

It doesnt seem to work any different from the first one, still cant delete the first input
it cantnot delete if only one row exixst. if you want to all row then simply remove condition "if($(".InputsWrapper").length > 1){" and its work. check it out

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.