Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to Add or Remove Dynamic TextBox using jQuery
WhatsApp
Upendra Pratap Shahi
10y
19.6
k
0
1
25
Blog
articleForCcshorpcornerDynamicTextb
Add jQuery library in your page
<script src=
"http://code.jquery.com/jquery-1.10.2.js"
></script>
Write some script for add or remove
<script type=
"text/javascript"
>
$(document).ready(
function
() {
$(
'#add'
).click(
function
() {
var
table = $(
this
).closest(
'table'
);
console.log(table.find(
'input:text'
).length);
if
(table.find(
'input:text'
).length < 50) {
var
x = $(
this
).closest(
'tr'
).nextAll(
'tr'
);
$.each(x,
function
(i, val) {
val.remove();
});
table.append('
<tr>
<td style=
"width:200px;"
align=
"right"
>First Name
<td>
<input type=
"text"
id=
"current Name"
value=
""
/>
</td>
<td style=
"width:200px;"
align=
"right"
>Last Name
<td>
<input type=
"text"
id=
"current Name"
value=
""
/>
</td>
</tr>');
$.each(x,
function
(i, val) {
table.append(val);
});
}
});
$(
'#del'
).click(
function
() {
var
table = $(
this
).closest(
'table'
);
if
(table.find(
'input:text'
).length > 1) {
table.find(
'input:text'
).last().closest(
'tr'
).remove();
}
});});
</script>
Then create design whether you want to show this.
<
table
border
=
"0"
cellspacing
=
"2"
>
<
tr
>
<
td
style
=
"width:200px;"
align
=
"right"
>
Name
<
td
>
<
input
type
=
"text"
id
=
"Job Name"
value
=
""
/>
</
td
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"right"
>
Versions
</
td
>
<
td
>
<
select
id
=
"version"
style
=
"width:350px;"
>
<
option
value
=
""
>
SELECT
</
option
>
</
select
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"right"
>
Test Scripts
</
td
>
<
td
>
<
select
id
=
"testscripts"
style
=
"width:350px;"
>
</
select
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"right"
>
datas
</
td
>
<
td
>
<
input
type
=
"button"
id
=
"add"
value
=
"Add"
/>
<
input
type
=
"button"
id
=
"del"
value
=
"Del"
/>
</
td
>
</
tr
>
<
tr
>
<
td
style
=
"height:3px"
colspan
=
"2"
>
</
td
>
</
tr
>
</
table
>
Now your code looks like the following:
AddTextbox page
<
html
>
<
head
>
<
script
src
=
"http://code.jquery.com/jquery-1.10.2.js"
>
</
script
>
<
script
type
=
"text/javascript"
>
$(document).ready(function () {
$('#add').click(function () {
var
table
= $(this).closest('table');
console.log(table.find('input:text').length);
if (table.find('input:text').length
<
50
) {
var
x
= $(this).closest('tr').nextAll('tr');
$.each(x, function (i, val) {
val.remove();
});
table.append('
<
tr
>
<
td
style
=
"width:200px;"
align
=
"right"
>
First Name
<
td
>
<
input
type
=
"text"
id
=
"current Name"
value
=
""
/>
</
td
>
<
td
style
=
"width:200px;"
align
=
"right"
>
Last Name
<
td
>
<
input
type
=
"text"
id
=
"current Name"
value
=
""
/>
</
td
>
</
tr
>
');
$.each(x, function (i, val) {
table.append(val);
});
}
});
$('#del').click(function () {
var
table
= $(this).closest('table');
if (table.find('input:text').length
>
1) {
table.find('input:text').last().closest('tr').remove();
}
});});
</
script
>
</
head
>
<
body
>
<
table
border
=
"0"
cellspacing
=
"2"
>
<
tr
>
<
td
style
=
"width:200px;"
align
=
"right"
>
Name
<
td
>
<
input
type
=
"text"
id
=
"Job Name"
value
=
""
/>
</
td
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"right"
>
Versions
</
td
>
<
td
>
<
select
id
=
"version"
style
=
"width:350px;"
>
<
option
value
=
""
>
SELECT
</
option
>
</
select
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"right"
>
Test Scripts
</
td
>
<
td
>
<
select
id
=
"testscripts"
style
=
"width:350px;"
>
</
select
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"right"
>
datas
</
td
>
<
td
>
<
input
type
=
"button"
id
=
"add"
value
=
"Add"
/>
<
input
type
=
"button"
id
=
"del"
value
=
"Del"
/>
</
td
>
</
tr
>
<
tr
>
<
td
style
=
"height:3px"
colspan
=
"2"
>
</
td
>
</
tr
>
</
table
>
</
body
>
</
html
>
Figure 1:
Output
Dynamic TextBox
jQuery
People also reading
Membership not found