After previously looking at this post, JQuery search in static HTML page with highlighting of found word, I have finally found what I was looking for. However, the search seems to break other HTML tags. I know it wasn't intended for my exact requirements but I'm looking for some help.
Here is a sample of HTML:
$('#searchfor').keyup(function(){
var page = $('#all_text');
var pageText = page.text().replace("<span>","").replace("</span>");
var searchedText = $('#searchfor').val();
var theRegEx = new RegExp("("+searchedText+")", "igm");
var newHtml = pageText.replace(theRegEx ,"<span>$1</span>");
page.html(newHtml);
});
#all_text span {
text-decoration: underline;
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<input type="text" id="searchfor" />
<ul id="all_text">
<li><a href="/somewhere">Somewhere</a></li>
<li><a href="/somewhere-else">Over there</a></li>
</ul>
After typing in the search box, it removes all <li>
tags and all <a>
tags. I'm not hugely confident with Javascript or Jquery so I can't figure this out for myself. It needs to retain the list and hyperlinks but only search in the visible text (i.e. not search in the href
field).
All input is greatly appreciated.