Skip to content

Instantly share code, notes, and snippets.

@nocodesupplyco
Created December 19, 2022 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nocodesupplyco/2753b0114a0a974c9b2c76d3787b9e8a to your computer and use it in GitHub Desktop.
Save nocodesupplyco/2753b0114a0a974c9b2c76d3787b9e8a to your computer and use it in GitHub Desktop.
Highlight Text From Search Query
<script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/jquery.mark.min.js" integrity="sha256-4HLtjeVgH0eIB3aZ9mLYF6E8oU5chNdjU6p6rrXpl9U=" crossorigin="anonymous"></script>
<script>
$(function() {
var mark = function() {
// Read the keyword
var keyword = $("input[name='keyword']").val();
// Determine selected options
var options = {};
$("input[name='opt[]']").each(function() {
options[$(this).val()] = $(this).is(":checked");
});
// Remove previous marked elements and mark
// the new keyword inside the context
$(".context").unmark({
done: function() {
$(".context").mark(keyword, options);
}
});
};
$("input[name='keyword']").on("input", mark);
$("input[type='checkbox']").on("change", mark);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment