Search the Community
Showing results for tags 'removeChild'.
-
Hi, I am trying to create a text tag where users can add comments, delete them and also choose where to insert that comment(if not default). My problem is how to give each new node a unique id, which should be a number 1, 2 and so on. Using these numbers, the user could select where to insert the new par or even to select which one to be deleted. To be honest I want to create a checkbox next to the new par, so the user can just thick which par to delete and then press the button, but I am open to better ideas:) For start could someone help me how to make it a create a unique id for each new nodes and how to call 2 var inside appendChild() Thanks <script> function addNew(){ var inputText=form.inputText.value var para=document.createElement("p"); var node=document.createTextNode(inputText); var x = document.createElement("INPUT"); x.setAttribute("type", "checkbox"); para.appendChild(node); // I tried para.appendChild(node, x); var element=document.getElementById("div1"); element.appendChild(para); // also element.appendChild(para, x); and all other variations. } function deleteComment(){ var parent=document.getElementById("div1"); var child=document.getElementById("ggg"); parent.removeChild(child); } /* * function addBefore(){ var inputText=form.inputText.value var para=document.createElement("p"); var node=document.createTextNode(inputText); para.appendChild(node); var element=document.getElementById("div1"); var child=document.getElementById("line"); element.insertBefore(para,child); } * */ </script> </head> <body> <form id="form"> <input type="text" value="inputText" id="inputText" /> <input type="button" value="Add New Comment" onclick="addNew()" /> <input type="button" value="Delete A Comment" onclick="deleteComment()" /> </form> <div id="div1"> <p id="ggg">gggggg</p> </div> </body>
- 18 replies
-
- appendChild
- removeChild
-
(and 2 more)
Tagged with: