Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Issue with passing variables

    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

    Code:
    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 );
    	}

  • #2
    New Coder
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can anyone help with this? Im struggling to grasp how I can pass a variable to the this. variables.

    Thanks

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Why not creating a custom property?
    Code:
    contentDiv.style.cursor = 'pointer';
    contentDiv.tab = contentDiv.id.substring( 15 );
    contentDiv.onclick= this.initialiseContent;
    Now the property should be found within the constructor as:
    Code:
    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?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •