www.webdeveloper.com
Results 1 to 2 of 2

Thread: Resolving Scope

  1. #1
    Join Date
    May 2009
    Posts
    46

    Resolving Scope

    I am developing a site that keeps track of my clients jobs. Each job shows in a list and has an arrow to the left of the job title that can be opened to reveal the job details.

    I'm creating the jobs in an Array of Objects. But I'm having a problem getting the arrow to link up to the proper Object. I'm going to simplify it in the code that I'm posting.

    I've had a similar problem that was resolved by putting the function inline. but for some reason it won't work this time.

    Any help would be greatly appreciated!

    Code:
    jobs = new Array();
    
    $(function(){
    	for(i=0; i<10; i++){
    		jobs[i] = Component(i);
    	}
    });
    	
    //-----------------------Job Object
    function Component(id){
    	this.id = id;
    	
    	this.header = header;
    	this.showBody = showBody;
    	
    	header();
    }
    
    function header(){
    	that = this;
    	
    	(function(x){
    		arrow = $("<a href='#"+x.id+"'>").html(">"+x.id).click(
    			function(){	alert(that.id);	}
    		);
    	})(that)
    	
    	$("#links").append(arrow);
    }
    
    function showBody(){
    	alert(this.id);	
    }
    In this sample, when I click on any of the anchor tags, I'm getting an alert that show "9". But tag shows correctly with each number (">1 >2 >3"..etc)

    Ultimately, I'd want the function inside the click to call the "showBody" function, but I'm not sure how to do that so that I can use "this" within the showBody function and still refer to the correct object.

    Thanks again,
    Brian

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    In the event, use "showBody(this);" and give the function an associated argument. Then alert(x.id);

    ^_^

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles