For a Google Chrome plugin, I am trying to scroll to a DOM element containing a given string. However, after filtering a selector, I am a little bit lost in the DOM.
$('*', 'body')
.andSelf()
.contents()
.filter(function(){
return this.nodeType === 3;
})
.filter(function(){
// Only match when contains given string anywhere in the text
if(this.nodeValue.indexOf(givenString != -1){
//idArr.push(this.offsetLeft);
var ele = $(this);
$('html, body').animate({
scrollTop: ele.offset().top
}, 2000);
return true;
}
return false;
});
Whenever I try to get the top offset of ele
, I get the top offset of the scrollbar in relation to the document instead. Any ideas? I thought it might be a scoping issues with my use of $this
inside the filter, but this
inside a filter should refer to the current DOM element.
EDIT:
by calling
var ele=$(this.parentNode);
I was able to get the element that contained the text node