admin管理员组文章数量:1134575
I have some code here that will do the following. The code creates an element "p" then I append it to a "div" in the HTML. I would like that "p" I just created have an unique identifier (ID) and set the name of the ID. So later on when the user wants to delete the last created element it will be able to get the ID so it can removeChild. Here is the code:
JavaScript:
var $ = function (id)
{
return document.getElementById(id);
}
function ShowResponse ()
{
var myResponse = $("myresponse").value;
var myPara = document.createElement("p");
var myDiv = $("mydiv");
myDiv.appendChild(myPara);
var myID = document.createElement("id");
myID.setAttribute("value", ID)
var myText = document.createTextNode(myResponse);
myPara.appendChild(myText);
}
function RemoveResponse ()
{
}
window.onload = function ()
{
$("showresponse").onclick = ShowResponse;
$("removeresponse").onclick = RemoveResponse;
}
HTML:
<body>
<div id="mydiv">
<h1>Practice</h1>
<p>Hi There</p>
<p>How are you?</p>
<p>
<input type="text" id="myresponse">
<br><input type="button" id="showresponse" value="Show Response">
<input type="button" id="removeresponse" value="Remove Response">
</p>
<hr>
</div>
</body>
I have some code here that will do the following. The code creates an element "p" then I append it to a "div" in the HTML. I would like that "p" I just created have an unique identifier (ID) and set the name of the ID. So later on when the user wants to delete the last created element it will be able to get the ID so it can removeChild. Here is the code:
JavaScript:
var $ = function (id)
{
return document.getElementById(id);
}
function ShowResponse ()
{
var myResponse = $("myresponse").value;
var myPara = document.createElement("p");
var myDiv = $("mydiv");
myDiv.appendChild(myPara);
var myID = document.createElement("id");
myID.setAttribute("value", ID)
var myText = document.createTextNode(myResponse);
myPara.appendChild(myText);
}
function RemoveResponse ()
{
}
window.onload = function ()
{
$("showresponse").onclick = ShowResponse;
$("removeresponse").onclick = RemoveResponse;
}
HTML:
<body>
<div id="mydiv">
<h1>Practice</h1>
<p>Hi There</p>
<p>How are you?</p>
<p>
<input type="text" id="myresponse">
<br><input type="button" id="showresponse" value="Show Response">
<input type="button" id="removeresponse" value="Remove Response">
</p>
<hr>
</div>
</body>
Share
Improve this question
edited Oct 21, 2017 at 9:04
ROMANIA_engineer
56.6k30 gold badges208 silver badges205 bronze badges
asked Oct 28, 2013 at 0:53
king_sw4king_sw4
1,2132 gold badges12 silver badges18 bronze badges
3 Answers
Reset to default 178Since id is an attribute don't create an id element, just do this:
myPara.setAttribute("id", "id_you_like");
You set an element's id by setting its corresponding property:
myPara.id = ID;
You can use dot notation.
myPara.id = 'anyNameYouLike';
本文标签: htmlJavaScript Adding an ID attribute to another created ElementStack Overflow
版权声明:本文标题:html - JavaScript Adding an ID attribute to another created Element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736862258a1955968.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论