Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing prototype in javascript
    text
    copied!<p>I have defined a couple of functions inside my javascript which work perfectly, but when I put it inside a prototype it just doesn't seem to work.</p> <p>wall.html:</p> <pre><code>&lt;script type="text/javascript" src="Jquery/jquery-1.4.4.js"&gt; &lt;/script&gt; &lt;script type="text/javascript" src="src/CommentManager.js"&gt; &lt;/script&gt; &lt;script type="text/javascript" src="src/Reply.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(){ CommentManager(); $("form#newMessage").submit(function(){ var message = $("input#newMessageTxt").val(); var newComment = new Comment(message); }); }); &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;/body&gt; </code></pre> <p>but the weird part is when I run the debugging tool in googlechrome, the $("form#newMessage").submit doesn't call at all. So Comment(message) is never created (which is where I have set up the prototype functions)</p> <p>Comment.js:</p> <pre><code>function Comment(message){ var self = this; var message = message; var comment = document.createElement("li"); comment.id = "comment"; comment.textContent = message; //empty reply field var replyField = document.createElement("ul"); replyField.id = "replyField"; //create the appropriate buttons createButtons(comment); //append the replyField comment.appendChild(replyField); //insert into wall addComment(comment); //effect after insertion Effect(comment); $(comment).mouseleave(function() {mouseOut(comment);}); $(comment).mouseenter(function() {mouseOver(comment);}); return comment; } Comment.prototype={ deleteComment : function (comment){ $(comment).fadeOut(); setTimeout(function() {comment.parentNode.removeChild(comment);},500); }, //there are more methods here } </code></pre> <p>Commentmanager.js:</p> <pre><code>function CommentManager(){ var owner = null; var wall = document.createElement("ul"); wall.id = "wall"; document.body.appendChild(wall); return wall; } function addComment(comment){ var wall = document.getElementById("wall"); wall.appendChild(comment); } </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