Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with javascript classes

Status
Not open for further replies.

JOSEMAOCU

Programmer
Joined
May 10, 2006
Messages
2
Location
ES
Hi to all,

Im making this small class:

function ReportItem()//sera un <table> con celdas
{
this.HTML="";
}
//data has an 2d array with headers and data to render a table
ReportItem.prototype.Test=function(data)
{
this.HTML="<div onclick=DoAny()>"+this.HTML"</div>";
document.write(this.HTML);
}
ReportItem.prototype.DoAny()=function()
{
alert(this.HTML);
}

//initialize the class

var ri= new ReportItem();
ri.Test('<table><tr><td>asdjasd</td></tr></table>');


My question is:

I can create an instance of the class, and execute the Test method without problems, but, when i click in the layer that the class generate i cant execute the DoAny method?

Could you give me a hand?
Thanks a lot.
Regards.
Josema.
 
Unless you want to make the function doAny() static, then you will need to extend your main object to include a reference to it's name (so that it knows what it is called when you document.write the markup).

Here is (untested) what I mean (my changes in red - and some simple error checking in blue):
Code:
function ReportItem()
{
   [COLOR=red]this.name="";[/color]
   this.HTML="";
}

ReportItem.prototype.Test=function(data)
{
    [COLOR=blue]if (this.name)[/color] this.HTML="<div onclick=[COLOR=red]"+this.name+".[/color]DoAny()>"+this.HTML"</div>";
    document.write(this.HTML);
}

//initialize the class
var ri= new ReportItem();
[COLOR=red]ri.name='ri';[/color]
Let us know how you go.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thanks for your fast and very useful response.

Cheers.
Josema.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top