Amongst other things, I have read:
but they haven't solved 'this' problem I'm having with a piece of JavaScript.
I have a Section object that gets passed some XML which it uses to populate the section. In the Section object I append a div
which has a specified index. The resulting jQuery object is pushed into a sections
Array. The following code is from the Section object code:
sections.push($('#section' + p_sectionIndex));
this.showSection = function() {
this.show();
}
this.hideSection = function() {
this.hide();
}
sections[sections.length-1].on('show', this.showSection.call(sections[sections.length-1]));
sections[sections.length-1].on('hide', this.hideSection.call(sections[sections.length-1]));
Elsewhere I call sections[index].trigger('hide');
and sections[index].trigger('show');
The first of the links I mentioned above seemed to suggest this
in a function depends on HOW it's called and that you could pass a reference to this
into the function by using call
. I know the showSection
and hideSection
function ARE being triggered - I just can't get the this
in those functions to refer to the jQuery objects in the sections
Array.
I have tried multiple variations of the above (excluding the call
, using $(this)
in the functions, adding the showSection
and hideSection
functions to the jQuery object - amongst others) but I'm kind of out of ideas.
Any help much appreciated!