TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Dynamic Create TextBox Control and Submit value using Javascript
Hemlata Kushwaha
Aug 10
2015
Code
4.3
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeFile=
"addcontrol.aspx.cs"
Inherits=
"addcontrol"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head runat=
"server"
>
<title></title>
<script src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
></script>
<script src=
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"
></script>
<script>
$(document).ready(function() {
var iCnt = 0;
// CREATE A "DIV" ELEMENT AND DESIGN IT USING JQUERY ".css()" CLASS.
var container = $(document.createElement(
'div'
)).css({
padding:
'5px'
, margin:
'20px'
, width:
'170px'
, border:
'1px dashed'
,
borderTopColor:
'#999'
, borderBottomColor:
'#999'
,
borderLeftColor:
'#999'
, borderRightColor:
'#999'
});
$(
'#btAdd'
).click(function() {
if
(iCnt <= 19) {
iCnt = iCnt + 1;
// ADD TEXTBOX.
$(container).append(
'<input type=text class="input" id=tb'
+ iCnt +
' value="Text Element '
+ iCnt +
'" />'
);
if
(iCnt == 1) {
// SHOW SUBMIT BUTTON IF ATLEAST "1" ELEMENT HAS BEEN CREATED.
var divSubmit = $(document.createElement(
'div'
));
$(divSubmit).append(
'<input type=button class="bt" onclick="GetTextValue()" id=btSubmit value=Submit />'
);
}
$(
'#main'
).after(container, divSubmit);
// ADD BOTH THE DIV ELEMENTS TO THE "main" CONTAINER.
}
else
{
// AFTER REACHING THE SPECIFIED LIMIT, DISABLE THE "ADD" BUTTON. (20 IS THE LIMIT WE HAVE SET)
$(container).append(
'<label>Reached the limit</label>'
);
$(
'#btAdd'
).attr(
'class'
,
'bt-disable'
);
$(
'#btAdd'
).attr(
'disabled'
,
'disabled'
);
}
});
$(
'#btRemove'
).click(function() {
// REMOVE ELEMENTS ONE PER CLICK.
if
(iCnt != 0) { $(
'#tb'
+ iCnt).remove(); iCnt = iCnt - 1; }
if
(iCnt == 0) { $(container).empty();
$(container).remove();
$(
'#btSubmit'
).remove();
$(
'#btAdd'
).removeAttr(
'disabled'
);
$(
'#btAdd'
).attr(
'class'
,
'bt'
)
}
});
$(
'#btRemoveAll'
).click(function() {
// REMOVE ALL THE ELEMENTS IN THE CONTAINER.
$(container).empty();
$(container).remove();
$(
'#btSubmit'
).remove(); iCnt = 0;
$(
'#btAdd'
).removeAttr(
'disabled'
);
$(
'#btAdd'
).attr(
'class'
,
'bt'
);
});
});
// PICK THE VALUES FROM EACH TEXTBOX WHEN "SUBMIT" BUTTON IS CLICKED.
var divValue, values =
''
;
function GetTextValue() {
$(divValue).empty();
$(divValue).remove(); values =
''
;
$(
'.input'
).each(function() {
divValue = $(document.createElement(
'div'
)).css({
padding:
'5px'
, width:
'200px'
});
values +=
this
.value +
'<br />'
});
$(divValue).append(
'<p><b>Your selected values</b></p>'
+ values);
$(
'body'
).append(divValue);
}
</script>
</head>
<body>
<form id=
"form1"
runat=
"server"
>
<div id=
"main"
>
<input type=
"button"
id=
"btAdd"
value=
"Add Element"
class
=
"bt"
/>
<input type=
"button"
id=
"btRemove"
value=
"Remove Element"
class
=
"bt"
/>
<input type=
"button"
id=
"btRemoveAll"
value=
"Remove All"
class
=
"bt"
/><br />
</div>
</form>
</body>
</html>
Dynamically Create TextBox
Dynamically RemoveTextBox