473,387 Members | 1,588 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,387 software developers and data experts.

Javascript class problem

107 100+
Hey,

This is probably just a question of syntax but i cant figure it out!

i have a javascript class with several functions, one being something like ...


Expand|Select|Wrap|Line Numbers
  1. TreeView.prototype.showItems = function(id) {
  2.  
  3.     itemImg = document.createElement("img");
  4.     itemImg.src = "images/treeplus.gif";
  5.     itemImg.onclick = function(){this.otherfunction();
  6.         document.appendChild(itemImg);
  7. }
  8.  
  9. TreeView.prototype.otherfunction = function(id) {
  10.          //do something
  11. }
In the following line:
Expand|Select|Wrap|Line Numbers
  1. itemImg.onclick = function(){this.otherfunction();
How do i make the otherfunction() part reference the current class, obviously this. doesnt work as the element is appended somewhere within the html doc and the onclick even looks for some global function called otherfunction, not within the TreeView class.

any ideas? (or a work around)

Ta

Andy
Feb 20 '08 #1
3 1051
gits
5,390 Expert Mod 4TB
hi ...

the simplest way to fix the scope would be:

Expand|Select|Wrap|Line Numbers
  1. var me = this;
  2.  
  3. itemImg.onclick = function() { me.otherfunction() };
kind regards
Feb 21 '08 #2
theS70RM
107 100+
dude i love u!

solved so many of my probs cheers.
Feb 25 '08 #3
gits
5,390 Expert Mod 4TB
no problem ;) ... post back to the forum anytime you have more questions ...

kind regards
Feb 25 '08 #4

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

Similar topics

6
by: Alex Fitzpatrick | last post by:
Just by way of introduction, I'm currently the principal developer and maintainer of the a JavaScript editor plug-in for Eclipse. https://sourceforge.net/projects/jseditor/ The plug-in as it...
2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
7
by: Kevin Newman | last post by:
I've been toying with a namespace manager, and wanted to get some input. So what do you think? if (typeof com == 'undefined') var com = {}; if (!com.unFocus) com.unFocus = {}; ...
11
by: trinitypete | last post by:
Hi all, I have a user control that uses control literal to build a heading with a link, and a div containing links below. As the link heading is hit, I want to change the style of the div,...
1
by: Jorge Ponte | last post by:
hi I have a Web User Control (ascx) - lets call it "My_WUC" - in a Web form. In that WUC I want have a textbox and a button. I want to click on the button and open a popup (I use javascript for...
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
8
by: rn5a | last post by:
I have gone through a no. of posts in this NewsGroup regarding my problem but alas, couldn't come across one which would have helped me in resolving the issue. My problem is this: An ASPX Form...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
3
by: mportman300 | last post by:
Have pity on me.. i have been doing html, css, javascript squarely over 2 years... and am now doing a family project.. in my project i have decided to use a context menu script, its a menu that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.