Link to home
Create AccountLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

Please explain this snippet of jQuery code

It's taken from jQuery in Action, page 61.

I read that the attr() function would call each element in the wrapped set, with the
return value of the function used as the attribute value.

1) I do not understand where previousValue comes from? Is jQuery passes this in like the value for index?

Please explain

2) would the value end in "and my name is unset" when this.id is null or just endefined?

thanks,
newbieweb
$('*').attr('title',function(index,previousValue) {
return previousValue + ' I am element ' + index +
' and my name is ' + (this.id || 'unset');
});

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kawzaki
kawzaki

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of curiouswebster

ASKER

1) Thanks. Now its perfecly clear that it gets the old argument value as a parameter. If the value is undefined, would it get an undefined value as a parameter? Would it throw an exception?

2) would the value be 'unset' if this.id is null?

SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks.
Avatar of kawzaki
kawzaki

u r welcome :)