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
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Mas
NA
478
70.8k
How to create the lable
Feb 11 2020 7:06 AM
Hello Members,
By click event, I am trying to create a label and text box, But it is opening both textboxes..
Here is the code.
<!DOCTYPE html>
<html>
<head>
<title>Dynamically Add Remove Table Rows
in
JavaScript</title> <style>
table {
width: 70%;
font: 17px Calibri;
}
table, th, td {
border: solid 1px #DDD;
border-collapse: collapse;
padding: 2px 3px;
text-align: center;
}
</style>
</head>
<body onload=
"createTable()"
> <p>
<input type=
"button"
id=
"addRow"
value=
"Add New Row"
onclick=
"addRow()"
/>
</p> <!--THE CONTAINER WHERE WE'll ADD THE DYNAMIC TABLE-->
<div id=
"cont"
></div> <p><input type=
"button"
id=
"bt"
value=
"Submit Data"
onclick=
"submit()"
/></p></body><script>
// ARRAY FOR HEADER.
var
arrHead =
new
Array();
arrHead = [
''
,
'Emp. Name'
,
'Designation'
,
'Age'
];
// SIMPLY ADD OR REMOVE VALUES IN THE ARRAY FOR TABLE HEADERS. // FIRST CREATE A TABLE STRUCTURE BY ADDING A FEW HEADERS AND
// ADD THE TABLE TO YOUR WEB PAGE.
function
createTable() {
var
empTable = document.createElement(
'table'
);
empTable.setAttribute(
'id'
,
'empTable'
);
// SET THE TABLE ID. var tr = empTable.insertRow(-1); for (var h = 0; h < arrHead.length; h++) {
var
th = document.createElement(
'th'
);
// TABLE HEADER.
th.innerHTML = arrHead[h];
tr.appendChild(th);
}
var
div = document.getElementById(
'cont'
);
div.appendChild(empTable);
// ADD THE TABLE TO YOUR WEB PAGE.
}
// ADD A NEW ROW TO THE TABLE.s
function
addRow() {
var
empTab = document.getElementById(
'empTable'
);
var
rowCnt = empTab.rows.length;
// GET TABLE ROW COUNT.
var
tr = empTab.insertRow(rowCnt);
// TABLE ROW.
tr = empTab.insertRow(rowCnt);
for
(
var
c = 0; c < arrHead.length; c++) {
var
td = document.createElement(
'td'
);
// TABLE DEFINITION.
td = tr.insertCell(c);
if
(c == 0) {
// FIRST COLUMN.
// ADD A BUTTON.
var
button = document.createElement(
'input'
);
// SET INPUT ATTRIBUTE.
button.setAttribute(
'type'
,
'button'
);
button.setAttribute(
'value'
,
'Remove'
);
// ADD THE BUTTON's 'onclick' EVENT.
button.setAttribute(
'onclick'
,
'removeRow(this)'
); td.appendChild(button);
}
else
{
// CREATE AND ADD TEXTBOX IN EACH CELL.
var
ele = document.createElement(
'input'
);
ele.setAttribute(
'type'
,
'text'
);
ele.setAttribute(
'value'
,
''
); td.appendChild(ele);
}
}
}
// DELETE TABLE ROW.
function
removeRow(oButton) {
var
empTab = document.getElementById(
'empTable'
);
empTab.deleteRow(oButton.parentNode.parentNode.rowIndex);
// BUTTON -> TD -> TR.
}
// EXTRACT AND SUBMIT TABLE DATA.
function
submit() {
var
myTab = document.getElementById(
'empTable'
);
var
values =
new
Array();
// LOOP THROUGH EACH ROW OF THE TABLE.
for
(row = 1; row < myTab.rows.length - 1; row++) {
for
(c = 0; c < myTab.rows[row].cells.length; c++) {
// EACH CELL IN A ROW. var element = myTab.rows.item(row).cells[c];
if
(element.childNodes[0].getAttribute(
'type'
) ==
'text'
) {
values.push(
"'"
+ element.childNodes[0].value +
"'"
);
}
}
}
// SHOW THE RESULT IN THE CONSOLE WINDOW.
console.log(values);
}
</script>
</html>
Can any one guide me here!
Thank you in advance!!
Reply
Answers (
1
)
aling link and image in same page
How to do dropDownlist using DataTable on Jquery?