...

View Full Version : Issue with passing variables



phil_b
12-01-2009, 10:32 PM
ive just started really to develop my javascript out of individual functions and appreciate and help with this. I start outside of the external JS file by:

reviews.init();
reviews.initialiseContent('comment');

This loads my data and loads + sets the comment tab as default. My problem is that in the external JS file (shown below) the loadTabs variable will not allow me to pass it a variable:

contentDiv.onclick = this.initialiseContent;

Whenever I pass a variable here it errors, am I setting this up correctly, should I be using prototype for my this. variables? Interested to hear back on if this structure of code is the right way to go about this and also how I can pass a variable in this way :)

Thanks, Phil


var reviews = new reviews();
var xmlhttp;
var classArray = Array('comment', 'review', 'video', 'stats', 'add');

function reviews(){

/*--------------------------------------------*/
// Setup tab events, rating and thumb content
/*--------------------------------------------*/

this.init = function()
{
/*--------------------------------------------*/
// Load and display tabs
/*--------------------------------------------*/

this.loadTabs();

/*--------------------------------------------*/
// Load and display rating
/*--------------------------------------------*/

this.loadRating();

/*--------------------------------------------*/
// Load and display thumbs
/*--------------------------------------------*/

this.loadThumbs();
}

/*--------------------------------------------*/
// Initialise and populate tab
/*--------------------------------------------*/

this.loadTabs = function()
{
for(key in classArray)
{
var contentDiv = document.getElementById( 'pp-content-tab-' + classArray[key] );

contentDiv.style.cursor = 'pointer';
contentDiv.onclick = this.initialiseContent;
}

document.getElementById('pp-content-tab-add').style.visibility = 'hidden';
document.getElementById('add-review').style.cursor = 'pointer';
document.getElementById('add-review').onclick = this.showElement;

}

this.showElement = function()
{

document.getElementById('pp-content-tab-add').style.visibility = 'visible';

this.id = 'pp-content-tab-add';
this.initialiseContent;
}

this.initialiseContent = function( tab )
{

if( this.id )
{
var tab = this.id.substring( 15 );
}

if( !in_array( tab, classArray ) )
{
var tab = 'comment';
}

for(key in classArray)
{
if( classArray[key] == tab )
{
document.getElementById( 'pp-content-tab-' + classArray[key] ).className = 'pp-tabon';

} else {

document.getElementById( 'pp-content-tab-' + classArray[key] ).className = 'pp-taboff';
}
}

sendrequest( tab, 'loadtab', stateChanged );
}

phil_b
12-02-2009, 11:47 AM
can anyone help with this? Im struggling to grasp how I can pass a variable to the this. variables.

Thanks

Kor
12-02-2009, 02:08 PM
Why not creating a custom property?


contentDiv.style.cursor = 'pointer';
contentDiv.tab = contentDiv.id.substring( 15 );
contentDiv.onclick= this.initialiseContent;

Now the property should be found within the constructor as:


this.initialiseContent = function(){
var tab=this.tab;
...
}

But, looking at your whole code: why do I have the sensation that you have complicated a simple thing?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum