1

Hello I want to do something like this : Here is example I want to do after click "+" add 2 new fields with indexed name+1, and after click "-" I want to delete this fields.

1
  • I don't know how to get started. I have form with one file input, but I don't know how add/delete dynamic next fields Commented Feb 28, 2014 at 9:55

2 Answers 2

1

Try this

var i=1;
$('#pl').click(function(){ $(this).parents('.ch').prepend($('<input/>').attr({'type':'file','name':i,'id':i}));
    i++;
});
$('#mn').click(function(){
    $(this).parents('.ch').children('input').first().remove();
i--;
})

If I understood your question properly It may help

live demo

Sign up to request clarification or add additional context in comments.

1 Comment

Almost, because I would like to be added once the two fields :) Thanks for the reply!
0

try this LIVE DEMO

HTML

<div id="main_div">

  <div id="div_1">
    <div style="float:left">
        <label>Textbox 1 </label><input type="text" name="text1" id="text1">
    </div>
    <div style="float:left">
        <label>Textbox 2 </label><input type="text" name="text2" id="text2">
    </div>
    <div style="float:left">
        <label class="cursor_plus" onclick="add_Textbox();">+</label><label class="cursor_minus" onclick="remove_Textbox();">-</label>
    </div>
  </div>

</div>

JAVASCRIPT :

<script>
var counter = 1;
function add_Textbox()
{
    counter = counter + 1;
    var second = counter *2;
        var first = second - 1;
        var html = '<div id="div_'+counter+'" style="clear:both;">';
            html += '<div style="float:left">';
            html += '<label>Textbox '+first+' </label><input type="text" name="text'+first+'" id="text'+first+'">';
            html += '</div>';
            html += '<div style="float:left">';
            html += '<label>Textbox '+second+' </label><input type="text" name="text'+second+'" id="text'+second+'">';
            html += '</div>';
            html += '<div style="float:left">';
            html += '<label class="cursor_plus" onclick="add_Textbox();">+</label><label class="cursor_minus" onclick="remove_Textbox();">-</label>';
            html += '</div>';
            html += '</div>';
    document.getElementById('main_div').innerHTML += html;      
}

function remove_Textbox()
{
    if(counter==1)
    {
        alert("Cant' remove remaining One Only !!");
    }
    else
    {
        var id_rem = "div_"+counter+"";
        (elem=document.getElementById(id_rem)).parentNode.removeChild(elem)
        counter = counter - 1;  
    }   
}

</script>

CSS :

<style>
.cursor_plus{float:left; font-weight:bold; color:green; cursor:pointer}
.cursor_minus{float:left;margin-left:5px;font-weight:bold; color:red; font-size:12px; cursor:pointer}
</style>

Comments

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.