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
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
9y
19.5
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
Up Next
Dynamically Adding And Removing TextBoxes On A Button Click Using jQuery
Ebook Download
View all
Frontend Developer Interview Questions and Answers
Read by 955 people
Download Now!
Learn
View all
Membership not found