473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Events & running object methods when they happen

6
Hello all,
I am new to JavaScript (having no programming background at all), trying to learn how to script myself. But I guess this is the barrier I will not get over without help :(
I did a lot of searching and tried everything I possibly could with no real results.
Let's get to the point: I understand, that when an event is fired, the 'this' keyword points to the element that triggered the event, in my case to a button. Now I want to run an object's method when the event happens:
My code:

Expand|Select|Wrap|Line Numbers
  1. function myObj() {};
  2.  
  3. myObj.prototype.myMethod = function() {
  4. alert('It works!');
  5. }
  6.  
  7. myObj.prototype.button = function() {
  8.     var ctrlInput = document.getElementById('button');    
  9.     var ctrlButton = document.createElement('button');
  10.     var ctrlStatus = document.createTextNode('Start');
  11.     ctrlInput.appendChild(ctrlButton);
  12.     ctrlButton.appendChild(ctrlStatus);
  13.  
  14.     ctrlButton.addEventListener('click', function() {
  15.         if(ctrlStatus.nodeValue == 'Start') {
  16.                         // fire myObj.myMethod();    
  17.                 ctrlStatus.nodeValue = 'Stop';
  18.         } else {
  19.                         // fire another method, which does not matter now  
  20.             ctrlStatus.nodeValue = 'Start';
  21.         }
  22.     }, false);
  23. }
  24.  
  25. aa = new myObj();
  26. aa.button();
Could anyone help me out?

jirka
Jul 20 '08 #1
3 995
mrhoo
428 256MB
You don't seem to have added the button to the document, and nodeValue is an integer, not the text content of an element.
Jul 21 '08 #2
rnd me
427 Expert 256MB
in a prototype method, this refers to the object to the left of the right-most dot.

however, when bound to an event, this means the element that fired the event.

in your object prototype code, use a line like "var that = this", and then you can refer to that.myMethod to hit the object, instead of using this.myMethod, which hits the element.
Jul 21 '08 #3
jirkap
6
in a prototype method,...
THANK YOU, now it works (I tried that workaround before and it did not, funny).

mrhoo> I did add the button, the HTML is not shown in the example above to make it easier to follow, however I realize it may be confusing.

I guess modyfing the value through nodeValue is okay, (refering to this: http://developer.mozilla.org/en/docs/DOM:element.nodeValue), as it is a text node. At least it works...

Thank you both.
Jul 21 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: V | last post by:
I have found that when I have a composite control that uses the CreateChildControls method, on a regular page load, Page_Load executes before CreateChildControls, but on a postback it is the...
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
1
by: Jack Addington | last post by:
I have a 3rd party object that fires an itemchanged event when someone edits some data on a form. This event has a custom eventArgs that has a field called ActionCode. In the code of the event,...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
2
by: Stampede | last post by:
Hi guys 'n' girls, I want to use callback methods when using BeginInvoke on some events. So far no problem, but know I thought about what could happen (if I'm not completly wrong). Lets say an...
2
by: elziko | last post by:
I have an object (Object1) that instantiates another class (Object2) in a seperate thread. It must be in its own thread for an ActiveX control it hosts to work. From Object1 I can get to all the...
3
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
20
by: David Levine | last post by:
I ran into a problem this morning with event accessor methods that appears to be a bug in C# and I am wondering if this a known issue. public event SomeDelegateSignature fooEvent; public event...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.