{"id":37552,"date":"2017-07-28T18:01:42","date_gmt":"2017-07-28T10:01:42","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=37552"},"modified":"2017-11-24T19:11:04","modified_gmt":"2017-11-24T11:11:04","slug":"markjs-search-highlighting","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/","title":{"rendered":"Add Search Keyword Highlighting to Any Webpage with Mark.js"},"content":{"rendered":"<p>Most browsers have the <strong><span class=\"key\">CTRL<\/span>+<span class=\"key\">F<\/span> functionality<\/strong> to search and find whatever the user is looking for. But, this feature <strong>isn\u2019t supported on mobile devices<\/strong> and it doesn\u2019t work well using dynamic text.<\/p>\n<p>Luckily, there\u2019s <strong><a target=\"_blank\" href=\"https:\/\/markjs.io\/\">Mark.js<\/a><\/strong>, a free JavaScript plugin that adds a <strong>highlight search feature<\/strong> to any page with ease.<\/p>\n<p>By default, it works as a <strong>vanilla JS plugin<\/strong> but can also run <strong>on top of jQuery<\/strong>. It\u2019s a completely open-source project, so you\u2019re free to use this on any website commercial or otherwise.<\/p>\n<p>It works much like any browser search feature, except it comes with extra goodies. You can add <strong>your own custom filters<\/strong> and search for words based on <strong>regular expressions<\/strong>, <strong>specific synonyms<\/strong>, and even in <strong>dynamic page elements<\/strong> such as iframes.<\/p>\n<p class=\"note\"><strong>Read Also:<\/strong> <a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/auto-highlight-text-user-click\/\">How to Auto-Highlight Text Upon User Click<\/a><\/p>\n<p>To get started, just download the Mark.js file <strong><a target=\"_blank\" href=\"https:\/\/github.com\/julmot\/mark.js\">from GitHub<\/a><\/strong> or host the file <strong><a target=\"_blank\" href=\"https:\/\/cdnjs.com\/libraries\/mark.js\/\">through a CDN<\/a><\/strong> to save time.<\/p>\n<p>You should run this function <strong>connected to an input field<\/strong> on the page. This way users can enter search terms and <strong>get immediate feedback<\/strong> via highlighted text.<\/p>\n<p>Here\u2019s a <strong>sample snippet<\/strong> from the demo page:<\/p>\n<pre>\r\n$(\".context\").mark(keyword [, options]);\r\n<\/pre>\n<p>The <code>.context<\/code> class targets wherever the function should <strong>search for terms<\/strong>. You might use the <strong>default HTML <code>&lt;body&gt;<\/code> element<\/strong> if you\u2019re trying to search the entire page, or you could pass <strong>multiple elements<\/strong> such as different tabbed widgets or iframes.<\/p>\n<p>Then, inside the <code>mark()<\/code> function you <strong>pass the keyword<\/strong>, along with the options (if you wish).<\/p>\n<p>If you let users type in a keyword then you can <strong>auto-update the function<\/strong> with a new keyword after every keypress. There\u2019s even a <a target=\"_blank\" href=\"https:\/\/api.jquery.com\/keyup\/\">specific function<\/a> to target this event.<\/p>\n<p>Mark.js <strong>works with all major browsers<\/strong>, including Chrome, Firefox, Opera (v12+), and Internet Explorer (9+). It\u2019s real easy to set up if you <a target=\"_blank\" href=\"https:\/\/markjs.io\/#getting-started\">follow the docs<\/a> and use the newest files.<\/p>\n<p>But, if you want to see <strong>Mark.js in action<\/strong> take a peek at the <strong><a target=\"_blank\" href=\"https:\/\/jsfiddle.net\/julmot\/vpav6tL1\/\">fiddle below<\/a><\/strong> using a very basic jQuery demo to search a few paragraphs of Lorem Ipsum.<\/p>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"300\" src=\"https:\/\/jsfiddle.net\/julmot\/vpav6tL1\/embedded\/\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n<p class=\"note\"><strong>Read Also:<\/strong> <a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/wordpress-search-plugin-snippet\/\">WordPress Search: Useful Plugins and Snippets<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Most browsers have the CTRL+F functionality to search and find whatever the user is looking for. But, this feature isn\u2019t supported on mobile devices and it doesn\u2019t work well using dynamic text. Luckily, there\u2019s Mark.js, a free JavaScript plugin that adds a highlight search feature to any page with ease. By default, it works as&hellip;<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3393],"tags":[3497,4117],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Add Search Keyword Highlighting to Any Webpage with Mark.js - Hongkiat<\/title>\n<meta name=\"description\" content=\"Most browsers have the CTRL+F functionality to search and find whatever the user is looking for. But, this feature isn&#039;t supported on mobile devices and\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Add Search Keyword Highlighting to Any Webpage with Mark.js\" \/>\n<meta property=\"og:description\" content=\"Most browsers have the CTRL+F functionality to search and find whatever the user is looking for. But, this feature isn&#039;t supported on mobile devices and\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2017-07-28T10:01:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-24T11:11:04+00:00\" \/>\n<meta name=\"author\" content=\"Jake Rocheleau\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jake Rocheleau\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/markjs-search-highlighting\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/markjs-search-highlighting\\\/\"},\"author\":{\"name\":\"Jake Rocheleau\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/966b2daea15283b4145e71aa98a82c2a\"},\"headline\":\"Add Search Keyword Highlighting to Any Webpage with Mark.js\",\"datePublished\":\"2017-07-28T10:01:42+00:00\",\"dateModified\":\"2017-11-24T11:11:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/markjs-search-highlighting\\\/\"},\"wordCount\":354,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"keywords\":[\"Javascript Library\",\"Javascripts\"],\"articleSection\":[\"Toolkit\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/markjs-search-highlighting\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/markjs-search-highlighting\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/markjs-search-highlighting\\\/\",\"name\":\"Add Search Keyword Highlighting to Any Webpage with Mark.js - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"datePublished\":\"2017-07-28T10:01:42+00:00\",\"dateModified\":\"2017-11-24T11:11:04+00:00\",\"description\":\"Most browsers have the CTRL+F functionality to search and find whatever the user is looking for. But, this feature isn't supported on mobile devices and\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/markjs-search-highlighting\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/markjs-search-highlighting\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/markjs-search-highlighting\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Add Search Keyword Highlighting to Any Webpage with Mark.js\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/966b2daea15283b4145e71aa98a82c2a\",\"name\":\"Jake Rocheleau\",\"description\":\"Jake is a writer and designer with over 10 years experience working on the web. He writes about user experience design and cool resources for designers\",\"sameAs\":[\"https:\\\/\\\/www.hongkiat.com\"],\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/jake\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Add Search Keyword Highlighting to Any Webpage with Mark.js - Hongkiat","description":"Most browsers have the CTRL+F functionality to search and find whatever the user is looking for. But, this feature isn't supported on mobile devices and","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/","og_locale":"en_US","og_type":"article","og_title":"Add Search Keyword Highlighting to Any Webpage with Mark.js","og_description":"Most browsers have the CTRL+F functionality to search and find whatever the user is looking for. But, this feature isn't supported on mobile devices and","og_url":"https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2017-07-28T10:01:42+00:00","article_modified_time":"2017-11-24T11:11:04+00:00","author":"Jake Rocheleau","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Jake Rocheleau","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/"},"author":{"name":"Jake Rocheleau","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/966b2daea15283b4145e71aa98a82c2a"},"headline":"Add Search Keyword Highlighting to Any Webpage with Mark.js","datePublished":"2017-07-28T10:01:42+00:00","dateModified":"2017-11-24T11:11:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/"},"wordCount":354,"commentCount":0,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"keywords":["Javascript Library","Javascripts"],"articleSection":["Toolkit"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/","url":"https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/","name":"Add Search Keyword Highlighting to Any Webpage with Mark.js - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"datePublished":"2017-07-28T10:01:42+00:00","dateModified":"2017-11-24T11:11:04+00:00","description":"Most browsers have the CTRL+F functionality to search and find whatever the user is looking for. But, this feature isn't supported on mobile devices and","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/markjs-search-highlighting\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Add Search Keyword Highlighting to Any Webpage with Mark.js"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/966b2daea15283b4145e71aa98a82c2a","name":"Jake Rocheleau","description":"Jake is a writer and designer with over 10 years experience working on the web. He writes about user experience design and cool resources for designers","sameAs":["https:\/\/www.hongkiat.com"],"url":"https:\/\/www.hongkiat.com\/blog\/author\/jake\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-9LG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/37552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=37552"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/37552\/revisions"}],"predecessor-version":[{"id":41956,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/37552\/revisions\/41956"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=37552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=37552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=37552"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=37552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}