$(document).ready(function(){
first();
$('#btnAdd').click(first);
$('#btnSend').click(send);
});
function first(){
var id = $('#cover div').length+1;
var wrapper = $("<div id=\"field"+id+"\">");
var parag = $("<p>phone number\""+id+"\"</p>");
var text = $("<input type='text' name=\"tel"+id+"\" />");
var btnDel = $("<input type='button' value='del' id=\"btn"+id+"\"/>");
btnDel.click(function(){
//$(this).parent().remove();
var no = parseInt($(this).parent().attr("id").replace("field",""));
delNode(no);
});
wrapper.append(parag);
wrapper.append(text);
wrapper.append(btnDel);
$('#cover').append(wrapper);
}
function delNode(no){
var i,n;
var count = $('#cover div').length;
$("#field"+no).remove();
for(i=no+1;i<=count;i++){
n = i-1;
console.log($("#field"+i));
$("#field"+i).attr("id","field"+n);
$("#field"+n+" p").text('phone number "'+n+'"');
$("[name=tel"+i+"]").attr("name","tel"+n);
$("[name=btn"+i+"]").attr("name","btn"+n);
}
}
function send(){
var id= $('#cover div').length;
var hiddens = $("<input type='hidden' name='hidden' value=\""+id+"\"/>");
$('form').append(hiddens);
$('form').submit();
}