{"id":284,"date":"2011-06-22T18:32:33","date_gmt":"2011-06-22T18:32:33","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=284"},"modified":"2011-06-22T18:32:33","modified_gmt":"2011-06-22T18:32:33","slug":"jquery-search-term-highlighting","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/jquery-search-term-highlighting\/","title":{"rendered":"jQuery search term highlighting"},"content":{"rendered":"<p>Highlighting keywords from a search can be done on the server-side, by using your development languages string replace functions, but this can add complexity to your code. This job is very well suited to client-side JavaScript. Using jQuery makes the job even easier.<\/p>\n<p>The ingredients:<\/p>\n<ol>\n<li>A jQuery function to wrap and add a css class to the keywords within your content. The function described here works well: <a class=\"target-blank\" href=\"http:\/\/wpswitch.com\/blog\/wordpress\/how-to-highlight-search-terms-with-jquery\/\">http:\/\/wpswitch.com\/blog\/wordpress\/how-to-highlight-search-terms-with-jquery\/<\/a><\/li>\n<li>A function to grab the keyword from the url query string.<\/li>\n<li>A little CSS to define how the highlighting will look.<\/li>\n<\/ol>\n<p>Put it all together:<\/p>\n<h3>The jQuery<\/h3>\n<p><code>\/\/ include function from step 1 above here<br \/>\n\tfunction getParameterByName( name ) {<br \/>\n\t&nbsp; name = name.replace(\/[\\[]\/,&quot;\\\\\\[&quot;).replace(\/[\\]]\/,&quot;\\\\\\]&quot;);<br \/>\n\t&nbsp; var regexS = &quot;[\\\\?&amp;]&quot;+name+&quot;=([^&amp;#]*)&quot;;<br \/>\n\t&nbsp; var regex = new RegExp( regexS );<br \/>\n\t&nbsp; var results = regex.exec( window.location.href );<br \/>\n\t&nbsp; if( results == null )<br \/>\n\t&nbsp;&nbsp;&nbsp; return &quot;&quot;;<br \/>\n\t&nbsp; else<br \/>\n\t&nbsp;&nbsp;&nbsp; return decodeURIComponent(results[1].replace(\/\\+\/g, &quot; &quot;));<br \/>\n\t}<br \/>\n\tvar searchQuery = getParameterByName(&#39;query&#39;);<br \/>\n\tif (searchQuery != &#39;&#39;)<br \/>\n\t&nbsp;&nbsp;&nbsp; $(&#39;#content&#39;).highlight(searchQuery, 1, &#39;hls&#39;);<br \/>\n\t<\/code><\/p>\n<p>&nbsp;<\/p>\n<h3>The CSS<\/h3>\n<p><code>.hls { padding: 0 4px; background: #ffa; } \/* highlight class *\/<br \/>\n\th3 .hls { background: none; }<br \/>\n\t<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Highlighting keywords from a search can be done on the server-side, by using your development languages string replace functions, but this can add complexity to&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[12,5],"tags":[86],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/284"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=284"}],"version-history":[{"count":1,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/284\/revisions"}],"predecessor-version":[{"id":285,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/284\/revisions\/285"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}