Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving a Javascript object from html file
    text
    copied!<p>Ok so I'm trying to remove an object called Comment from the main Html file. Here's the code:</p> <pre><code>function Comment(message){ var self = this; var child = null; var message = message; var comment = document.createElement("div"); comment.id = "comment"; comment.textContent = message; var replyField = document.createElement("ul"); var reply = document.createElement("input"); reply.type = "submit"; reply.value = "reply"; comment.appendChild(reply); var deleteBtn = document.createElement("input"); deleteBtn.type = "submit"; deleteBtn.value = "delete"; deleteBtn.addEventListener("click", function(comment){deleteComment(comment)},false); comment.appendChild(deleteBtn); comment.appendChild(replyField); return comment; } function deleteComment(comment){ var parent = document.getElementById("wall"); parent.removeChild(comment); } </code></pre> <p>Once I have created the "Comment object" and prepended it to a <code>&lt;ul id="wall"&gt;</code>, the delete button does not work for some reason. Can Anyone help me? </p> <p>here is the html code:</p> <pre><code>&lt;head&gt; &lt;script type="text/javascript" src="Jquery/jquery-1.4.4.js"&gt; &lt;/script&gt; &lt;script type="text/javascript" src="src/Comment.js"&gt; &lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ $("form#newMessage").submit(function(){ var message = $("input#newMessageTxt").val(); var newComment = new Comment(message); var commentHtml = newComment.outerHTML; $("ul#wall").prepend('&lt;li style="display: none;"&gt;'+commentHtml+"&lt;/li&gt;"); $("ul#wall li:first").fadeIn(); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="message"&gt; &lt;form id="newMessage"&amp;gt;&gt; &lt;input type="text" id="newMessageTxt" height="200px" value="Write a message" onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;" /&gt; &lt;input type="submit" value="Submit"&gt;&lt;/button&gt; &lt;/form&gt; &lt;/div&gt; &lt;ul id="wall"&gt; &lt;/ul&gt; &lt;/body&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload