/    Sign up×
Community /Pin to ProfileBookmark

Referencing Function Within Object

I’m having some trouble, with the below code. I keep getting the error “this.SomeTxt” is not a function. If anyone could take a look at this and let me know what im doing wrong.

[CODE]
var MyNewObj = new Test(‘random string’);
MyNewObj.prntTxt();

function Test(Txt) {

this.Txt = ‘No string specified’;

this.prntTxt= function() {
document.onmousedown = this.Func1;
}

this.Func1= function(e) {
if(e==null) {
e = window.event;
}
if(e.target!=null) {
var MyElem = e.target;
} else {
var MyElem = e.srcElement;
}
if(MyElem.style.className = ‘PrntNow’)
this.SomeTxt(‘test’);
}

this.SomeTxt = function(MyTxt) {
document.write(MyTxt);
}
}[/CODE]

Editted to fix typo…

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@toicontienDec 08.2006 — The this.SomeTxt() function uses document.write(). You cannot use document.write() once the document has finished loading. You'll have to use document.getElementById('foo') to grab a reference to an HTML tag, and use the .innerHTML DOM property to write the text to the tag.
this.SomeTxt = function(MyTxt) {
document.getElementById('someId').innerHTML = MyTxt;
}

On top of that, the 'm' and the 'e' in document are transposed in the code you posted, which is a syntax error.
Copy linkTweet thisAlerts:
@iceman2gauthorDec 08.2006 — I replaced document.write with alert to test things. And still get the same error.
Copy linkTweet thisAlerts:
@iceman2gauthorDec 09.2006 — Anymore suggestions?
Copy linkTweet thisAlerts:
@iceman2gauthorDec 09.2006 — Any reason why I would be getting the error [b] "this.SomeTxt" is not a function[/b]
Copy linkTweet thisAlerts:
@iceman2gauthorDec 10.2006 — Anyone???
Copy linkTweet thisAlerts:
@toicontienDec 11.2006 — The following code works for me:
<i>
</i>function Test(Txt) {
this.Txt = 'No string specified';

this.prntTxt= function() {
document.onmousedown = this.Func1;
}

this.Func1= function(e) {
if(e==null) {
e = window.event;
}
if(e.target!=null) {
var MyElem = e.target;
} else {
var MyElem = e.srcElement;
}
if(MyElem.style.className = 'PrntNow')
this.SomeTxt('test');
}

this.SomeTxt = function(MyTxt) {
alert(MyTxt);
}
}

var foo = new Test('booyah!');
foo.SomeTxt('yeah buddy!');

In order to use the functions inside Text(), you actually have to use the "new" keyword in front of the function name. Text() is actually a class constructor in this case. Once you've instantiated the class, you can use the member functions like SomeText();
×

Success!

Help @iceman2g spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...