<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
head
runat
"server"
title
></
</
body
form
id
"form1"
div
script
type
"text/javascript"
src
"http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"
$(function () {
$("#btnAdd").bind("click", function () {
var div = $("<
/>");
div.html(AddDDL());
div.html(GetDynamicTextBox(''));
$("#TextBoxContainer").append(div);
});
$("#btnGet").bind("click", function () {
var values = "";
$("input[name=DynamicTextBox]").each(function () {
values += $(this).val() + "\n";
alert(values);
$("body").on("click", ".remove", function () {
var index = $(this).closest("div").index();
index = parseInt(index / 2) + 1;
$(this).closest("div").remove();
var ddlId = index;
$('[id$=ddl' + parseInt(ddlId) + ']').remove();
var i = 1;
var Ids = [];
$('[id*=ddl]').each(function () {
$(this).attr("id", "ddl" + i);
Ids.push(i);
i++;
$('[id*=hfDDLId]').val(parseInt($('[id*=hfDDLId]').val()) - 1);
var resultIds = Ids.join(',');
$('[id*=hfDropDownIds]').val(resultIds);
function AddDDL() {
var Id = parseInt($('[id*=hfDDLId]').val()) + 1;
var ddlId = "ddl" + (Id);
//var optionList = [1, 2, 3, 4, 5];
//$('[id*=div1]').append(getDropDownList(ddlId, ddlId, optionList));
$("#TextBoxContainer").append(getDropDownList(ddlId, ddlId));
$('[id*=hfDDLId]').val(Id);
var previousDropDownId = $('[id*=hfDropDownIds]').val();
if (previousDropDownId != '') {
$('[id*=hfDropDownIds]').val(previousDropDownId + ',' + Id);
} else {
$('[id*=hfDropDownIds]').val(Id);
}
return false;
function getDropDownList(name, id) {
var combo = $("<
select
>").attr("id", id).attr("name", name).attr("runat", "server").attr("class", "class").attr("required", "required");
//$.each(optionList, function (i, el) {
combo.append("<
option
value
" + "
" + ">" + "Select Category" + "</
>");
1" + ">" + "1" + "</
2" + ">" + "2" + "</
3" + ">" + "3" + "</
//});
return combo;
function GetDynamicTextBox(value) {
return '<
input
name
"DynamicTextBox"
"text"
"' + value + '"
required/> '
+ '<
"button"
"Remove"
class
"remove"
/>'
"btnAdd"
"Add"
/>
br
"TextBoxContainer"
<!--Textboxes will be added here -->
"hidden"
"hfSelectedValue"
"hfDropDownIds"
""
"btnGet"
"Get Values"
"hfDDLId"
"0"