admin管理员组文章数量:1415145
I have created a div in HTML and want to add inner div dynamically to it. Below is the code for HTML:
<div id="table">
<div class="row">
This is the Demo First row Content.
<div class="cell1">
Cell 1 Content
</div>
<div class="cell2">
Cell 2 Content
</div>
</div>
<div class="row">
This is the Demo Second row Content.
<div class="cell1">
Cell 1 Content
</div>
<div class="cell2">
Cell 2 Content
</div>
</div>
</div>
<input type="button" value="Add New Row" onclick="addNewRow()" />
My CSS is:
div {
border: 1px dotted red;
padding: 10px;
}
And I have done the JavaScript for it but it is not working. The JavaScript is:
function addNewRow {
var table = document.getElementById('table');
var rDiv = document.createElement('div');
table.appendChild(rDiv);
rDiv.innerHTML = "This is the Demo Third row Content.";
var c1Div = document.createElement('div');
rDiv.appendChild(c1Div);
c1Div.innerHTML = "Cell 1 Content";
var c2Div = document.createElement('div');
rDiv.appendChild(c2Div);
c2Div.innerHTML = "Cell 2 Content";
}
But when I executed it, the new rows are not added. Please guide me what I am missing.
I have created a div in HTML and want to add inner div dynamically to it. Below is the code for HTML:
<div id="table">
<div class="row">
This is the Demo First row Content.
<div class="cell1">
Cell 1 Content
</div>
<div class="cell2">
Cell 2 Content
</div>
</div>
<div class="row">
This is the Demo Second row Content.
<div class="cell1">
Cell 1 Content
</div>
<div class="cell2">
Cell 2 Content
</div>
</div>
</div>
<input type="button" value="Add New Row" onclick="addNewRow()" />
My CSS is:
div {
border: 1px dotted red;
padding: 10px;
}
And I have done the JavaScript for it but it is not working. The JavaScript is:
function addNewRow {
var table = document.getElementById('table');
var rDiv = document.createElement('div');
table.appendChild(rDiv);
rDiv.innerHTML = "This is the Demo Third row Content.";
var c1Div = document.createElement('div');
rDiv.appendChild(c1Div);
c1Div.innerHTML = "Cell 1 Content";
var c2Div = document.createElement('div');
rDiv.appendChild(c2Div);
c2Div.innerHTML = "Cell 2 Content";
}
But when I executed it, the new rows are not added. Please guide me what I am missing.
Share Improve this question edited Aug 4, 2022 at 16:01 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 18, 2013 at 14:13 MKDMKD 7992 gold badges11 silver badges15 bronze badges3 Answers
Reset to default 2You have a typo preventing the execution of the function.
The correct name is document.getElementById
(line 2 in JS).
You might want to enable the console (F12 IE debugger, Firebug, Developer Tools, etc) for debugging next time. These kinds of errors are very easy to spot.
Here is a working JsBin: http://jsbin./egImArO/1/edit
define javascript function like function addNewRow() { not a function addNewRow {
Just replace first js code line with this one:-
function addNewRow(){
本文标签: Dynamically append Div in HTML JavaScriptStack Overflow
版权声明:本文标题:Dynamically append Div in HTML JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745209952a2647822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论