Mr. Developer Mr. Developer - 5 months ago 40
Javascript Question

How to bold searched content using only javascript?

I'm trying to make an auto suggest search using javascript. All things are working fine, now i wanted to make searched text bold in the list.

Is this possible when user search something then only search text become bold in the result list. For example if i search

then one will be bold in the list.

var inputId = 'filter-search';
var itemsData = 'filter-value';
var displaySet = false;
var displayArr = [];

function getDisplayType(element) {
var elementStyle = element.currentStyle || window.getComputedStyle(element, "");
return elementStyle.display;

document.getElementById(inputId).onkeyup = function() {
var searchVal = this.value.toLowerCase();
var filterItems = document.querySelectorAll('[' + itemsData + ']');
for(var i = 0; i < filterItems.length; i++) {
if (!displaySet) {
filterItems[i].style.display = 'none';
if(filterItems[i].getAttribute('filter-value').toUpperCase().indexOf(searchVal.toUpperCase()) >= 0) {
filterItems[i].style.display = displayArr[i];
displaySet = true;

<input type="text" id="filter-search" />

<li filter-value="One Is">One Is (Uppercase)</li>
<li filter-value="one is">one is (Lowercase)</li>
<li filter-value="two">Two</li>
<li filter-value="three">Three</li>
<li filter-value="four">Four</li>
<li filter-value="five" >Five</li>
<li filter-value="six">Six</li>
<li filter-value="seven">Seven</li>
<li filter-value="eight">Eight</li>
<li filter-value="nine">Nine</li>
<li filter-value="ten" >Ten</li>


I have done this via using below code

var textcontent = filterItems[i].textContent;
    var replacedval = "<strong>"+currval+"</strong>"
    var finalval = textcontent.replace(currval, replacedval);
   filterItems[i].innerHTML = finalval;
        filterItems[i].style.display = 'none';

Here is working JSFiddle