$(document).ready(function() { $('.loading_panel').hide(); var MaxInputs = 10; //maximum input boxes allowed var InputsWrapper = $("#InputsWrapper"); //Input boxes wrapper ID var AddButton = $("#AddMoreWWWBox"); //Add button ID var x = InputsWrapper.length; //initlal text box count var FieldCount=1; //to keep track of text box added var weblists = $.parseJSON($("input[name='websites_list']").val()); // get and decode json object $.each(weblists, function(idx, obj) { // add existing elements FieldCount++; //text box added increment //add input box if(idx == 0 ){ // to first element $( "div input[name='websites[]']" ).first().val(obj.www); $( "div input[name='websites_id[]']" ).first().val(obj.id); } else { // rest $(InputsWrapper).append(''); } x++; //text box increment }); $(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(',/tr>'); x++; //text box increment } return false; }); $("body").on("click",".removeclass", function(e){ //user click on remove text if( x > 1 ) { $(this).closest('tr').remove();//remove text box x--; //decrement textbox } return false; }) });