/    Sign up×
Community /Pin to ProfileBookmark

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);
}[/CODE]

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

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeFeb 18.2011 — In the event, use "showBody(this);" and give the function an associated argument. Then alert(x.id);

^_^
×

Success!

Help @bcmann 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.8,
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,
)...