I have a basic, case sensitive, term specific search with the code below. It will work for now but I would like something that (In order of importance):
1: ignores case (ie "hi" and "Hi" are both the same. toLowerCase
is not an option and is not the same thing)
2: Will yield a hit if the search query is 'Search Term' and the searched string is 'searching terms', as an example.
3: Searches the entire string even after finding a hit for more hits.
The purpose is to search a <p>
tag with a specific id
for a term. If it has it then display it. Ultimately, I will use this in a loop that will search many <p>
tags and display the ones with hits and leave hidden the ones without.
CODE:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to locate where in the string a specifed value occurs.</p>
<p id="demo1" style="display:none;">Hello world, welcome to the universe.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var x = document.getElementById("demo1")
var str = x.innerHTML.toString();
var n = str.indexOf("welcome");
if (n != -1) {
x.style.display = 'inline';
} else {
x.innerHTML = 'Negative';
x.style.display = 'inline';
}
}
</script>
</body>
</html>