-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindtextindocument.js
More file actions
30 lines (30 loc) · 1.03 KB
/
findtextindocument.js
File metadata and controls
30 lines (30 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
let foundTxtPosition = 0;
const classTxtFoundInSearch = 'txt-found-in-search';
const findInDocument = (positionSelection) => {
const positionToFind = positionSelection.position;
let txt = document.querySelector('#txt-to-find').value;
const cleanFind = () => {
$("*").removeClass(classTxtFoundInSearch);
}
if(txt.trim() == ""){
cleanFind();
return;
}
let $txtElement = $(`p:contains('${txt}'), span:contains('${txt}')`);
foundTxtPosition = foundTxtPosition + (positionToFind);
if(foundTxtPosition == 0 || !$txtElement.length || $txtElement[foundTxtPosition - 1] >= $txtElement.length){
cleanFind();
}
$txtElement = $txtElement[foundTxtPosition];
if(typeof($txtElement) === "undefined"){
foundTxtPosition = foundTxtPosition - (positionToFind);
}else{
cleanFind();
$($txtElement).addClass(classTxtFoundInSearch);
if($($txtElement).length){
let txtElementTopPosition = $($txtElement).offset().top;
window.scrollTo(0, txtElementTopPosition);
}
}
}
/* Gabriel Tessarini */