Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I run an object's method onEvent in javascript?
    primarykey
    data
    text
    <p>I just started using javascript and I'm missing something important in my knowledge. I was hoping you could help me fill in the gap.</p> <p>So the script I'm trying to run is suppose to count the characters in a text field, and update a paragraph to tell the user how many characters they have typed. I have an object called charCounter. sourceId is the id of the text area to count characters in. statusId is the id of the paragraph to update everytime a key is pressed.</p> <pre><code>function charCounter(sourceId, statusId) { this.sourceId = sourceId; this.statusId = statusId; this.count = 0; } </code></pre> <p>There is one method called updateAll. It updates the count of characters and updates the paragraph.</p> <pre><code>charCounter.prototype.updateAll = function() { //get the character count; //change the paragraph; } </code></pre> <p>I have a start function that is called when the window loads.</p> <pre><code>function start() { //This is the problem document.getElementbyId('mytextfield').onkeydown = myCounter.updateAll; document.getElementbyId('mytextfield').onkeyup = myCounter.updateAll; } myCounter = new charCounter("mytextfield","charcount"); window.onload = start; </code></pre> <p>The above code is the problem. Why in the world can't I call the myCounter.updateAll method when the event is fired? This is really confusing to me. I understand that if you call a method <b>likeThis()</b> you'll get a value from the function. If you call it <b>likeThis</b> you are getting a pointer to a function. I'm pointing my event to a function. I've also tried calling the function straight up and it works just fine, but it will not work when the event is fired.</p> <p>What am I missing?</p> <hr> <p>Thanks for all the answers. Here's three different implementations.</p> <h1>Implementation 1</h1> <pre><code>function CharCounter(sourceId, statusId) { this.sourceId = sourceId; this.statusId = statusId; this.count = 0; }; CharCounter.prototype.updateAll = function() { this.count = document.getElementById(this.sourceId).value.length; document.getElementById(this.statusId).innerHTML = "There are "+this.count+" charactors"; }; function start() { myCharCounter.updateAll(); document.getElementById('mytextfield').onkeyup = function() { myCharCounter.updateAll(); }; document.getElementById('mytextfield').onkeydown = function() { myCharCounter.updateAll(); }; }; myCharCounter = new CharCounter('mytextfield','charcount'); window.onload = start; </code></pre> <h1>Implementation 2</h1> <pre><code>function CharCounter(sourceId, statusId) { this.sourceId = sourceId; this.statusId = statusId; this.count = 0; }; CharCounter.prototype.updateAll = function() { this.count = document.getElementById(this.sourceId).value.length; document.getElementById(this.statusId).innerHTML = "There are "+ this.count+" charactors"; }; CharCounter.prototype.start = function() { var instance = this; instance.updateAll(); document.getElementById(this.sourceId).onkeyup = function() { instance.updateAll(); }; document.getElementById(this.sourceId).onkeydown = function() { instance.updateAll(); }; }; window.onload = function() { var myCounter = new CharCounter("mytextfield","charcount"); myCounter.start(); }; </code></pre> <h1>Implementation 3</h1> <pre><code>function CharCounter(sourceId, statusId) { this.sourceId = sourceId; this.statusId = statusId; this.count = 0; }; CharCounter.prototype.updateAll = function() { this.count = document.getElementById(this.sourceId).value.length; document.getElementById(this.statusId).innerHTML = "There are "+this.count+" charactors"; }; function bind(funcToCall, desiredThisValue) { return function() { funcToCall.apply(desiredThisValue); }; }; function start() { myCharCounter.updateAll(); document.getElementById('mytextfield').onkeyup = bind(myCharCounter.updateAll, myCharCounter); document.getElementById('mytextfield').onkeydown = bind(myCharCounter.updateAll, myCharCounter); }; myCharCounter = new CharCounter('mytextfield','charcount'); window.onload = start; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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