How to highlight Jquery Autocomplete matched word only?










0















I am using Jquery Autocomplete to search form a list,and it works fine,But all i need to hightlight background of matched word in the list,how can i dot this ?



This is what i have tried so far.
fiddle link



 var availableTags = [
'DHA ABC',
'DHA BB',
'DHA CC',
];


$('#tags').autocomplete(

source: availableTags,
search: function(event, ui)
$('#wrapper').empty();
,
select: function(e, ui)
$(this).val(ui.item.name);
return false;
,
).data('autocomplete')._renderItem = function(ul, item)
matchFound = 0;
var cls = this.options.highlightClass;
var inp = $("#tags").val().split(" ");
var template = "<span class='" + cls + "'>$1</span>";
var items = item.label.split(" ");
for (var i = 0; i < items.length; i++)
for (var j = 0; j < inp.length; j++)
index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
if (index > -1)
matchFound += 1;


if (matchFound == inp.length)
return $('<div class="element"></div>')
.data('item.autocomplete', item.label)
.append('<a href="#">' + item.label + '</a>')
.appendTo($('#wrapper'));
break;



;









share|improve this question




























    0















    I am using Jquery Autocomplete to search form a list,and it works fine,But all i need to hightlight background of matched word in the list,how can i dot this ?



    This is what i have tried so far.
    fiddle link



     var availableTags = [
    'DHA ABC',
    'DHA BB',
    'DHA CC',
    ];


    $('#tags').autocomplete(

    source: availableTags,
    search: function(event, ui)
    $('#wrapper').empty();
    ,
    select: function(e, ui)
    $(this).val(ui.item.name);
    return false;
    ,
    ).data('autocomplete')._renderItem = function(ul, item)
    matchFound = 0;
    var cls = this.options.highlightClass;
    var inp = $("#tags").val().split(" ");
    var template = "<span class='" + cls + "'>$1</span>";
    var items = item.label.split(" ");
    for (var i = 0; i < items.length; i++)
    for (var j = 0; j < inp.length; j++)
    index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
    if (index > -1)
    matchFound += 1;


    if (matchFound == inp.length)
    return $('<div class="element"></div>')
    .data('item.autocomplete', item.label)
    .append('<a href="#">' + item.label + '</a>')
    .appendTo($('#wrapper'));
    break;



    ;









    share|improve this question


























      0












      0








      0








      I am using Jquery Autocomplete to search form a list,and it works fine,But all i need to hightlight background of matched word in the list,how can i dot this ?



      This is what i have tried so far.
      fiddle link



       var availableTags = [
      'DHA ABC',
      'DHA BB',
      'DHA CC',
      ];


      $('#tags').autocomplete(

      source: availableTags,
      search: function(event, ui)
      $('#wrapper').empty();
      ,
      select: function(e, ui)
      $(this).val(ui.item.name);
      return false;
      ,
      ).data('autocomplete')._renderItem = function(ul, item)
      matchFound = 0;
      var cls = this.options.highlightClass;
      var inp = $("#tags").val().split(" ");
      var template = "<span class='" + cls + "'>$1</span>";
      var items = item.label.split(" ");
      for (var i = 0; i < items.length; i++)
      for (var j = 0; j < inp.length; j++)
      index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
      if (index > -1)
      matchFound += 1;


      if (matchFound == inp.length)
      return $('<div class="element"></div>')
      .data('item.autocomplete', item.label)
      .append('<a href="#">' + item.label + '</a>')
      .appendTo($('#wrapper'));
      break;



      ;









      share|improve this question
















      I am using Jquery Autocomplete to search form a list,and it works fine,But all i need to hightlight background of matched word in the list,how can i dot this ?



      This is what i have tried so far.
      fiddle link



       var availableTags = [
      'DHA ABC',
      'DHA BB',
      'DHA CC',
      ];


      $('#tags').autocomplete(

      source: availableTags,
      search: function(event, ui)
      $('#wrapper').empty();
      ,
      select: function(e, ui)
      $(this).val(ui.item.name);
      return false;
      ,
      ).data('autocomplete')._renderItem = function(ul, item)
      matchFound = 0;
      var cls = this.options.highlightClass;
      var inp = $("#tags").val().split(" ");
      var template = "<span class='" + cls + "'>$1</span>";
      var items = item.label.split(" ");
      for (var i = 0; i < items.length; i++)
      for (var j = 0; j < inp.length; j++)
      index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
      if (index > -1)
      matchFound += 1;


      if (matchFound == inp.length)
      return $('<div class="element"></div>')
      .data('item.autocomplete', item.label)
      .append('<a href="#">' + item.label + '</a>')
      .appendTo($('#wrapper'));
      break;



      ;






      jquery jquery-ui-autocomplete






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 12:34









      Saurabh Solanki

      1,450825




      1,450825










      asked Nov 13 '18 at 9:44









      Khirad BanuKhirad Banu

      2981417




      2981417






















          3 Answers
          3






          active

          oldest

          votes


















          1














          I created sample fiddle for your case with key function for highliting



          function highlite(label, substring) 
          var start = label.toLowerCase().indexOf(substring.toLowerCase());
          var end = start + substring.length;
          label = label.substr(0, start) +
          '<span class="highlite">' +
          label.substr(start, end - start) +
          '</span>' +
          label.substr(end);
          return label;







          share|improve this answer


















          • 1





            Thank you,this is what i was looking far

            – Khirad Banu
            Nov 14 '18 at 6:26


















          1

















          var availableTags = [
          'DHA 11 Rahbar',
          'DHA City',
          'DHA Defence',
          'Dhalloke',
          'Dharampura',
          'DHA 11 Rahbar Phase 1',
          'DHA 11 Rahbar Phase 2',
          'DHA 11 Rahbar Phase 2 Extension',
          'DHA 9 Town',
          'DHA Phase 1',
          'DHA Phase 10',
          'DHA Phase 10',
          'DHA Phase 11 - Halloki Gardens',
          'DHA Phase 2',
          'DHA Phase 3',
          'DHA Phase 4',
          'DHA Phase 5',
          'DHA Phase 6',
          'DHA Phase 7',
          'DHA Phase 8',
          'DHA Phase 9 Prism',
          'DHA - EME Cottages'

          ];

          $('#tags').autocomplete(

          source: availableTags,
          search: function(event, ui)
          $('#wrapper').empty();
          ,
          select: function(e, ui)
          $(this).val(ui.item.name);
          return false;
          ,
          )

          .data('autocomplete')._renderItem = function(ul, item)
          matchFound = 0;
          var cls = this.options.highlightClass;
          var inp = $("#tags").val().split(" ");
          var template = "<span class='" + cls + "'>$1</span>";
          var items = item.label.split(" ");
          for (var i = 0; i < items.length; i++)
          for (var j = 0; j < inp.length; j++)
          index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
          if (index > -1)
          matchFound += 1;


          if (matchFound == inp.length)

          var str=item.label;
          str = str.replace(inp[0], '<span style="color: yellow;">'+inp[0]+'</span>');
          // console.log(inp);
          return $('<div class="element">'+str+'</div>')
          .data('item.autocomplete', str)
          // .append('<a href="#">' + item.label + '</a>')
          .appendTo($('#wrapper'));


          ;

          body 
          font-size: 12px;


          #wrapper
          margin-top: 50px;


          .element
          border: 1px solid PowderBlue;


          .ui-autocomplete
          display: none !important;

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js"></script>

          <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/sunny/jquery-ui.css" rel="stylesheet"/>
          <label for="tags">Tags:</label>
          <input id="tags" />
          <div id="wrapper"></div>





          fiddle : http://jsfiddle.net/sumeshnu/gwethjq0/



          Replace the matched characters from the label with background color using a span tag



          enter image description here






          share|improve this answer

























          • Its not working,in your fiddle as well,span is not appending with matched list

            – Khirad Banu
            Nov 13 '18 at 10:20











          • @KhiradBanu It is working. It is case sensitive now.I got console error from the internal library.

            – Sumesh TG
            Nov 13 '18 at 10:23











          • Thanks its working now,Can we remove this case sensitive ?

            – Khirad Banu
            Nov 13 '18 at 10:27


















          0














          very simple you just need to set an attribute to div like below



          <div class="element" style="background-color:green; width:30%;"></div>


          your code should be something like this.



          var availableTags = [
          'DHA 11 Rahbar',
          'DHA City',
          'DHA Defence',
          'Dhalloke',
          'Dharampura',
          'DHA 11 Rahbar Phase 1',
          'DHA 11 Rahbar Phase 2',
          'DHA 11 Rahbar Phase 2 Extension',
          'DHA 9 Town',
          'DHA Phase 1',
          'DHA Phase 10',
          'DHA Phase 10',
          'DHA Phase 11 - Halloki Gardens',
          'DHA Phase 2',
          'DHA Phase 3',
          'DHA Phase 4',
          'DHA Phase 5',
          'DHA Phase 6',
          'DHA Phase 7',
          'DHA Phase 8',
          'DHA Phase 9 Prism',
          'DHA - EME Cottages'

          ];

          $('#tags').autocomplete(

          source: availableTags,
          search: function(event, ui)
          $('#wrapper').empty();
          ,
          select: function(e, ui)
          $(this).val(ui.item.name);
          return false;
          ,
          )

          .data('autocomplete')._renderItem = function(ul, item)
          matchFound = 0;
          var cls = this.options.highlightClass;
          var inp = $("#tags").val().split(" ");
          var template = "<span class='" + cls + "'>$1</span>";
          var items = item.label.split(" ");
          for (var i = 0; i < items.length; i++)
          for (var j = 0; j < inp.length; j++)
          index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
          if (index > -1)
          matchFound += 1;


          if (matchFound == inp.length)
          return $('<div class="element" style="background-color:green; width:30%;"></div>')
          .data('item.autocomplete', item.label)
          .append('<a href="javascript:void(0);">' + item.label + '</a>')
          .appendTo($('#wrapper'));
          break;



          ;


          check this fiddle






          share|improve this answer























          • No what i mean to highlight just matched word,not entire list.Have a look at this image ibb.co/kCTYSq

            – Khirad Banu
            Nov 13 '18 at 9:55












          • just matched... its only renderd that items which is matched.

            – Negi Rox
            Nov 13 '18 at 9:57











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278081%2fhow-to-highlight-jquery-autocomplete-matched-word-only%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          I created sample fiddle for your case with key function for highliting



          function highlite(label, substring) 
          var start = label.toLowerCase().indexOf(substring.toLowerCase());
          var end = start + substring.length;
          label = label.substr(0, start) +
          '<span class="highlite">' +
          label.substr(start, end - start) +
          '</span>' +
          label.substr(end);
          return label;







          share|improve this answer


















          • 1





            Thank you,this is what i was looking far

            – Khirad Banu
            Nov 14 '18 at 6:26















          1














          I created sample fiddle for your case with key function for highliting



          function highlite(label, substring) 
          var start = label.toLowerCase().indexOf(substring.toLowerCase());
          var end = start + substring.length;
          label = label.substr(0, start) +
          '<span class="highlite">' +
          label.substr(start, end - start) +
          '</span>' +
          label.substr(end);
          return label;







          share|improve this answer


















          • 1





            Thank you,this is what i was looking far

            – Khirad Banu
            Nov 14 '18 at 6:26













          1












          1








          1







          I created sample fiddle for your case with key function for highliting



          function highlite(label, substring) 
          var start = label.toLowerCase().indexOf(substring.toLowerCase());
          var end = start + substring.length;
          label = label.substr(0, start) +
          '<span class="highlite">' +
          label.substr(start, end - start) +
          '</span>' +
          label.substr(end);
          return label;







          share|improve this answer













          I created sample fiddle for your case with key function for highliting



          function highlite(label, substring) 
          var start = label.toLowerCase().indexOf(substring.toLowerCase());
          var end = start + substring.length;
          label = label.substr(0, start) +
          '<span class="highlite">' +
          label.substr(start, end - start) +
          '</span>' +
          label.substr(end);
          return label;








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 16:28









          dganencodganenco

          22118




          22118







          • 1





            Thank you,this is what i was looking far

            – Khirad Banu
            Nov 14 '18 at 6:26












          • 1





            Thank you,this is what i was looking far

            – Khirad Banu
            Nov 14 '18 at 6:26







          1




          1





          Thank you,this is what i was looking far

          – Khirad Banu
          Nov 14 '18 at 6:26





          Thank you,this is what i was looking far

          – Khirad Banu
          Nov 14 '18 at 6:26













          1

















          var availableTags = [
          'DHA 11 Rahbar',
          'DHA City',
          'DHA Defence',
          'Dhalloke',
          'Dharampura',
          'DHA 11 Rahbar Phase 1',
          'DHA 11 Rahbar Phase 2',
          'DHA 11 Rahbar Phase 2 Extension',
          'DHA 9 Town',
          'DHA Phase 1',
          'DHA Phase 10',
          'DHA Phase 10',
          'DHA Phase 11 - Halloki Gardens',
          'DHA Phase 2',
          'DHA Phase 3',
          'DHA Phase 4',
          'DHA Phase 5',
          'DHA Phase 6',
          'DHA Phase 7',
          'DHA Phase 8',
          'DHA Phase 9 Prism',
          'DHA - EME Cottages'

          ];

          $('#tags').autocomplete(

          source: availableTags,
          search: function(event, ui)
          $('#wrapper').empty();
          ,
          select: function(e, ui)
          $(this).val(ui.item.name);
          return false;
          ,
          )

          .data('autocomplete')._renderItem = function(ul, item)
          matchFound = 0;
          var cls = this.options.highlightClass;
          var inp = $("#tags").val().split(" ");
          var template = "<span class='" + cls + "'>$1</span>";
          var items = item.label.split(" ");
          for (var i = 0; i < items.length; i++)
          for (var j = 0; j < inp.length; j++)
          index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
          if (index > -1)
          matchFound += 1;


          if (matchFound == inp.length)

          var str=item.label;
          str = str.replace(inp[0], '<span style="color: yellow;">'+inp[0]+'</span>');
          // console.log(inp);
          return $('<div class="element">'+str+'</div>')
          .data('item.autocomplete', str)
          // .append('<a href="#">' + item.label + '</a>')
          .appendTo($('#wrapper'));


          ;

          body 
          font-size: 12px;


          #wrapper
          margin-top: 50px;


          .element
          border: 1px solid PowderBlue;


          .ui-autocomplete
          display: none !important;

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js"></script>

          <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/sunny/jquery-ui.css" rel="stylesheet"/>
          <label for="tags">Tags:</label>
          <input id="tags" />
          <div id="wrapper"></div>





          fiddle : http://jsfiddle.net/sumeshnu/gwethjq0/



          Replace the matched characters from the label with background color using a span tag



          enter image description here






          share|improve this answer

























          • Its not working,in your fiddle as well,span is not appending with matched list

            – Khirad Banu
            Nov 13 '18 at 10:20











          • @KhiradBanu It is working. It is case sensitive now.I got console error from the internal library.

            – Sumesh TG
            Nov 13 '18 at 10:23











          • Thanks its working now,Can we remove this case sensitive ?

            – Khirad Banu
            Nov 13 '18 at 10:27















          1

















          var availableTags = [
          'DHA 11 Rahbar',
          'DHA City',
          'DHA Defence',
          'Dhalloke',
          'Dharampura',
          'DHA 11 Rahbar Phase 1',
          'DHA 11 Rahbar Phase 2',
          'DHA 11 Rahbar Phase 2 Extension',
          'DHA 9 Town',
          'DHA Phase 1',
          'DHA Phase 10',
          'DHA Phase 10',
          'DHA Phase 11 - Halloki Gardens',
          'DHA Phase 2',
          'DHA Phase 3',
          'DHA Phase 4',
          'DHA Phase 5',
          'DHA Phase 6',
          'DHA Phase 7',
          'DHA Phase 8',
          'DHA Phase 9 Prism',
          'DHA - EME Cottages'

          ];

          $('#tags').autocomplete(

          source: availableTags,
          search: function(event, ui)
          $('#wrapper').empty();
          ,
          select: function(e, ui)
          $(this).val(ui.item.name);
          return false;
          ,
          )

          .data('autocomplete')._renderItem = function(ul, item)
          matchFound = 0;
          var cls = this.options.highlightClass;
          var inp = $("#tags").val().split(" ");
          var template = "<span class='" + cls + "'>$1</span>";
          var items = item.label.split(" ");
          for (var i = 0; i < items.length; i++)
          for (var j = 0; j < inp.length; j++)
          index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
          if (index > -1)
          matchFound += 1;


          if (matchFound == inp.length)

          var str=item.label;
          str = str.replace(inp[0], '<span style="color: yellow;">'+inp[0]+'</span>');
          // console.log(inp);
          return $('<div class="element">'+str+'</div>')
          .data('item.autocomplete', str)
          // .append('<a href="#">' + item.label + '</a>')
          .appendTo($('#wrapper'));


          ;

          body 
          font-size: 12px;


          #wrapper
          margin-top: 50px;


          .element
          border: 1px solid PowderBlue;


          .ui-autocomplete
          display: none !important;

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js"></script>

          <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/sunny/jquery-ui.css" rel="stylesheet"/>
          <label for="tags">Tags:</label>
          <input id="tags" />
          <div id="wrapper"></div>





          fiddle : http://jsfiddle.net/sumeshnu/gwethjq0/



          Replace the matched characters from the label with background color using a span tag



          enter image description here






          share|improve this answer

























          • Its not working,in your fiddle as well,span is not appending with matched list

            – Khirad Banu
            Nov 13 '18 at 10:20











          • @KhiradBanu It is working. It is case sensitive now.I got console error from the internal library.

            – Sumesh TG
            Nov 13 '18 at 10:23











          • Thanks its working now,Can we remove this case sensitive ?

            – Khirad Banu
            Nov 13 '18 at 10:27













          1












          1








          1










          var availableTags = [
          'DHA 11 Rahbar',
          'DHA City',
          'DHA Defence',
          'Dhalloke',
          'Dharampura',
          'DHA 11 Rahbar Phase 1',
          'DHA 11 Rahbar Phase 2',
          'DHA 11 Rahbar Phase 2 Extension',
          'DHA 9 Town',
          'DHA Phase 1',
          'DHA Phase 10',
          'DHA Phase 10',
          'DHA Phase 11 - Halloki Gardens',
          'DHA Phase 2',
          'DHA Phase 3',
          'DHA Phase 4',
          'DHA Phase 5',
          'DHA Phase 6',
          'DHA Phase 7',
          'DHA Phase 8',
          'DHA Phase 9 Prism',
          'DHA - EME Cottages'

          ];

          $('#tags').autocomplete(

          source: availableTags,
          search: function(event, ui)
          $('#wrapper').empty();
          ,
          select: function(e, ui)
          $(this).val(ui.item.name);
          return false;
          ,
          )

          .data('autocomplete')._renderItem = function(ul, item)
          matchFound = 0;
          var cls = this.options.highlightClass;
          var inp = $("#tags").val().split(" ");
          var template = "<span class='" + cls + "'>$1</span>";
          var items = item.label.split(" ");
          for (var i = 0; i < items.length; i++)
          for (var j = 0; j < inp.length; j++)
          index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
          if (index > -1)
          matchFound += 1;


          if (matchFound == inp.length)

          var str=item.label;
          str = str.replace(inp[0], '<span style="color: yellow;">'+inp[0]+'</span>');
          // console.log(inp);
          return $('<div class="element">'+str+'</div>')
          .data('item.autocomplete', str)
          // .append('<a href="#">' + item.label + '</a>')
          .appendTo($('#wrapper'));


          ;

          body 
          font-size: 12px;


          #wrapper
          margin-top: 50px;


          .element
          border: 1px solid PowderBlue;


          .ui-autocomplete
          display: none !important;

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js"></script>

          <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/sunny/jquery-ui.css" rel="stylesheet"/>
          <label for="tags">Tags:</label>
          <input id="tags" />
          <div id="wrapper"></div>





          fiddle : http://jsfiddle.net/sumeshnu/gwethjq0/



          Replace the matched characters from the label with background color using a span tag



          enter image description here






          share|improve this answer


















          var availableTags = [
          'DHA 11 Rahbar',
          'DHA City',
          'DHA Defence',
          'Dhalloke',
          'Dharampura',
          'DHA 11 Rahbar Phase 1',
          'DHA 11 Rahbar Phase 2',
          'DHA 11 Rahbar Phase 2 Extension',
          'DHA 9 Town',
          'DHA Phase 1',
          'DHA Phase 10',
          'DHA Phase 10',
          'DHA Phase 11 - Halloki Gardens',
          'DHA Phase 2',
          'DHA Phase 3',
          'DHA Phase 4',
          'DHA Phase 5',
          'DHA Phase 6',
          'DHA Phase 7',
          'DHA Phase 8',
          'DHA Phase 9 Prism',
          'DHA - EME Cottages'

          ];

          $('#tags').autocomplete(

          source: availableTags,
          search: function(event, ui)
          $('#wrapper').empty();
          ,
          select: function(e, ui)
          $(this).val(ui.item.name);
          return false;
          ,
          )

          .data('autocomplete')._renderItem = function(ul, item)
          matchFound = 0;
          var cls = this.options.highlightClass;
          var inp = $("#tags").val().split(" ");
          var template = "<span class='" + cls + "'>$1</span>";
          var items = item.label.split(" ");
          for (var i = 0; i < items.length; i++)
          for (var j = 0; j < inp.length; j++)
          index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
          if (index > -1)
          matchFound += 1;


          if (matchFound == inp.length)

          var str=item.label;
          str = str.replace(inp[0], '<span style="color: yellow;">'+inp[0]+'</span>');
          // console.log(inp);
          return $('<div class="element">'+str+'</div>')
          .data('item.autocomplete', str)
          // .append('<a href="#">' + item.label + '</a>')
          .appendTo($('#wrapper'));


          ;

          body 
          font-size: 12px;


          #wrapper
          margin-top: 50px;


          .element
          border: 1px solid PowderBlue;


          .ui-autocomplete
          display: none !important;

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js"></script>

          <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/sunny/jquery-ui.css" rel="stylesheet"/>
          <label for="tags">Tags:</label>
          <input id="tags" />
          <div id="wrapper"></div>





          fiddle : http://jsfiddle.net/sumeshnu/gwethjq0/



          Replace the matched characters from the label with background color using a span tag



          enter image description here






          var availableTags = [
          'DHA 11 Rahbar',
          'DHA City',
          'DHA Defence',
          'Dhalloke',
          'Dharampura',
          'DHA 11 Rahbar Phase 1',
          'DHA 11 Rahbar Phase 2',
          'DHA 11 Rahbar Phase 2 Extension',
          'DHA 9 Town',
          'DHA Phase 1',
          'DHA Phase 10',
          'DHA Phase 10',
          'DHA Phase 11 - Halloki Gardens',
          'DHA Phase 2',
          'DHA Phase 3',
          'DHA Phase 4',
          'DHA Phase 5',
          'DHA Phase 6',
          'DHA Phase 7',
          'DHA Phase 8',
          'DHA Phase 9 Prism',
          'DHA - EME Cottages'

          ];

          $('#tags').autocomplete(

          source: availableTags,
          search: function(event, ui)
          $('#wrapper').empty();
          ,
          select: function(e, ui)
          $(this).val(ui.item.name);
          return false;
          ,
          )

          .data('autocomplete')._renderItem = function(ul, item)
          matchFound = 0;
          var cls = this.options.highlightClass;
          var inp = $("#tags").val().split(" ");
          var template = "<span class='" + cls + "'>$1</span>";
          var items = item.label.split(" ");
          for (var i = 0; i < items.length; i++)
          for (var j = 0; j < inp.length; j++)
          index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
          if (index > -1)
          matchFound += 1;


          if (matchFound == inp.length)

          var str=item.label;
          str = str.replace(inp[0], '<span style="color: yellow;">'+inp[0]+'</span>');
          // console.log(inp);
          return $('<div class="element">'+str+'</div>')
          .data('item.autocomplete', str)
          // .append('<a href="#">' + item.label + '</a>')
          .appendTo($('#wrapper'));


          ;

          body 
          font-size: 12px;


          #wrapper
          margin-top: 50px;


          .element
          border: 1px solid PowderBlue;


          .ui-autocomplete
          display: none !important;

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js"></script>

          <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/sunny/jquery-ui.css" rel="stylesheet"/>
          <label for="tags">Tags:</label>
          <input id="tags" />
          <div id="wrapper"></div>





          var availableTags = [
          'DHA 11 Rahbar',
          'DHA City',
          'DHA Defence',
          'Dhalloke',
          'Dharampura',
          'DHA 11 Rahbar Phase 1',
          'DHA 11 Rahbar Phase 2',
          'DHA 11 Rahbar Phase 2 Extension',
          'DHA 9 Town',
          'DHA Phase 1',
          'DHA Phase 10',
          'DHA Phase 10',
          'DHA Phase 11 - Halloki Gardens',
          'DHA Phase 2',
          'DHA Phase 3',
          'DHA Phase 4',
          'DHA Phase 5',
          'DHA Phase 6',
          'DHA Phase 7',
          'DHA Phase 8',
          'DHA Phase 9 Prism',
          'DHA - EME Cottages'

          ];

          $('#tags').autocomplete(

          source: availableTags,
          search: function(event, ui)
          $('#wrapper').empty();
          ,
          select: function(e, ui)
          $(this).val(ui.item.name);
          return false;
          ,
          )

          .data('autocomplete')._renderItem = function(ul, item)
          matchFound = 0;
          var cls = this.options.highlightClass;
          var inp = $("#tags").val().split(" ");
          var template = "<span class='" + cls + "'>$1</span>";
          var items = item.label.split(" ");
          for (var i = 0; i < items.length; i++)
          for (var j = 0; j < inp.length; j++)
          index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
          if (index > -1)
          matchFound += 1;


          if (matchFound == inp.length)

          var str=item.label;
          str = str.replace(inp[0], '<span style="color: yellow;">'+inp[0]+'</span>');
          // console.log(inp);
          return $('<div class="element">'+str+'</div>')
          .data('item.autocomplete', str)
          // .append('<a href="#">' + item.label + '</a>')
          .appendTo($('#wrapper'));


          ;

          body 
          font-size: 12px;


          #wrapper
          margin-top: 50px;


          .element
          border: 1px solid PowderBlue;


          .ui-autocomplete
          display: none !important;

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js"></script>

          <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/sunny/jquery-ui.css" rel="stylesheet"/>
          <label for="tags">Tags:</label>
          <input id="tags" />
          <div id="wrapper"></div>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 10:28

























          answered Nov 13 '18 at 10:08









          Sumesh TGSumesh TG

          2,1331825




          2,1331825












          • Its not working,in your fiddle as well,span is not appending with matched list

            – Khirad Banu
            Nov 13 '18 at 10:20











          • @KhiradBanu It is working. It is case sensitive now.I got console error from the internal library.

            – Sumesh TG
            Nov 13 '18 at 10:23











          • Thanks its working now,Can we remove this case sensitive ?

            – Khirad Banu
            Nov 13 '18 at 10:27

















          • Its not working,in your fiddle as well,span is not appending with matched list

            – Khirad Banu
            Nov 13 '18 at 10:20











          • @KhiradBanu It is working. It is case sensitive now.I got console error from the internal library.

            – Sumesh TG
            Nov 13 '18 at 10:23











          • Thanks its working now,Can we remove this case sensitive ?

            – Khirad Banu
            Nov 13 '18 at 10:27
















          Its not working,in your fiddle as well,span is not appending with matched list

          – Khirad Banu
          Nov 13 '18 at 10:20





          Its not working,in your fiddle as well,span is not appending with matched list

          – Khirad Banu
          Nov 13 '18 at 10:20













          @KhiradBanu It is working. It is case sensitive now.I got console error from the internal library.

          – Sumesh TG
          Nov 13 '18 at 10:23





          @KhiradBanu It is working. It is case sensitive now.I got console error from the internal library.

          – Sumesh TG
          Nov 13 '18 at 10:23













          Thanks its working now,Can we remove this case sensitive ?

          – Khirad Banu
          Nov 13 '18 at 10:27





          Thanks its working now,Can we remove this case sensitive ?

          – Khirad Banu
          Nov 13 '18 at 10:27











          0














          very simple you just need to set an attribute to div like below



          <div class="element" style="background-color:green; width:30%;"></div>


          your code should be something like this.



          var availableTags = [
          'DHA 11 Rahbar',
          'DHA City',
          'DHA Defence',
          'Dhalloke',
          'Dharampura',
          'DHA 11 Rahbar Phase 1',
          'DHA 11 Rahbar Phase 2',
          'DHA 11 Rahbar Phase 2 Extension',
          'DHA 9 Town',
          'DHA Phase 1',
          'DHA Phase 10',
          'DHA Phase 10',
          'DHA Phase 11 - Halloki Gardens',
          'DHA Phase 2',
          'DHA Phase 3',
          'DHA Phase 4',
          'DHA Phase 5',
          'DHA Phase 6',
          'DHA Phase 7',
          'DHA Phase 8',
          'DHA Phase 9 Prism',
          'DHA - EME Cottages'

          ];

          $('#tags').autocomplete(

          source: availableTags,
          search: function(event, ui)
          $('#wrapper').empty();
          ,
          select: function(e, ui)
          $(this).val(ui.item.name);
          return false;
          ,
          )

          .data('autocomplete')._renderItem = function(ul, item)
          matchFound = 0;
          var cls = this.options.highlightClass;
          var inp = $("#tags").val().split(" ");
          var template = "<span class='" + cls + "'>$1</span>";
          var items = item.label.split(" ");
          for (var i = 0; i < items.length; i++)
          for (var j = 0; j < inp.length; j++)
          index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
          if (index > -1)
          matchFound += 1;


          if (matchFound == inp.length)
          return $('<div class="element" style="background-color:green; width:30%;"></div>')
          .data('item.autocomplete', item.label)
          .append('<a href="javascript:void(0);">' + item.label + '</a>')
          .appendTo($('#wrapper'));
          break;



          ;


          check this fiddle






          share|improve this answer























          • No what i mean to highlight just matched word,not entire list.Have a look at this image ibb.co/kCTYSq

            – Khirad Banu
            Nov 13 '18 at 9:55












          • just matched... its only renderd that items which is matched.

            – Negi Rox
            Nov 13 '18 at 9:57
















          0














          very simple you just need to set an attribute to div like below



          <div class="element" style="background-color:green; width:30%;"></div>


          your code should be something like this.



          var availableTags = [
          'DHA 11 Rahbar',
          'DHA City',
          'DHA Defence',
          'Dhalloke',
          'Dharampura',
          'DHA 11 Rahbar Phase 1',
          'DHA 11 Rahbar Phase 2',
          'DHA 11 Rahbar Phase 2 Extension',
          'DHA 9 Town',
          'DHA Phase 1',
          'DHA Phase 10',
          'DHA Phase 10',
          'DHA Phase 11 - Halloki Gardens',
          'DHA Phase 2',
          'DHA Phase 3',
          'DHA Phase 4',
          'DHA Phase 5',
          'DHA Phase 6',
          'DHA Phase 7',
          'DHA Phase 8',
          'DHA Phase 9 Prism',
          'DHA - EME Cottages'

          ];

          $('#tags').autocomplete(

          source: availableTags,
          search: function(event, ui)
          $('#wrapper').empty();
          ,
          select: function(e, ui)
          $(this).val(ui.item.name);
          return false;
          ,
          )

          .data('autocomplete')._renderItem = function(ul, item)
          matchFound = 0;
          var cls = this.options.highlightClass;
          var inp = $("#tags").val().split(" ");
          var template = "<span class='" + cls + "'>$1</span>";
          var items = item.label.split(" ");
          for (var i = 0; i < items.length; i++)
          for (var j = 0; j < inp.length; j++)
          index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
          if (index > -1)
          matchFound += 1;


          if (matchFound == inp.length)
          return $('<div class="element" style="background-color:green; width:30%;"></div>')
          .data('item.autocomplete', item.label)
          .append('<a href="javascript:void(0);">' + item.label + '</a>')
          .appendTo($('#wrapper'));
          break;



          ;


          check this fiddle






          share|improve this answer























          • No what i mean to highlight just matched word,not entire list.Have a look at this image ibb.co/kCTYSq

            – Khirad Banu
            Nov 13 '18 at 9:55












          • just matched... its only renderd that items which is matched.

            – Negi Rox
            Nov 13 '18 at 9:57














          0












          0








          0







          very simple you just need to set an attribute to div like below



          <div class="element" style="background-color:green; width:30%;"></div>


          your code should be something like this.



          var availableTags = [
          'DHA 11 Rahbar',
          'DHA City',
          'DHA Defence',
          'Dhalloke',
          'Dharampura',
          'DHA 11 Rahbar Phase 1',
          'DHA 11 Rahbar Phase 2',
          'DHA 11 Rahbar Phase 2 Extension',
          'DHA 9 Town',
          'DHA Phase 1',
          'DHA Phase 10',
          'DHA Phase 10',
          'DHA Phase 11 - Halloki Gardens',
          'DHA Phase 2',
          'DHA Phase 3',
          'DHA Phase 4',
          'DHA Phase 5',
          'DHA Phase 6',
          'DHA Phase 7',
          'DHA Phase 8',
          'DHA Phase 9 Prism',
          'DHA - EME Cottages'

          ];

          $('#tags').autocomplete(

          source: availableTags,
          search: function(event, ui)
          $('#wrapper').empty();
          ,
          select: function(e, ui)
          $(this).val(ui.item.name);
          return false;
          ,
          )

          .data('autocomplete')._renderItem = function(ul, item)
          matchFound = 0;
          var cls = this.options.highlightClass;
          var inp = $("#tags").val().split(" ");
          var template = "<span class='" + cls + "'>$1</span>";
          var items = item.label.split(" ");
          for (var i = 0; i < items.length; i++)
          for (var j = 0; j < inp.length; j++)
          index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
          if (index > -1)
          matchFound += 1;


          if (matchFound == inp.length)
          return $('<div class="element" style="background-color:green; width:30%;"></div>')
          .data('item.autocomplete', item.label)
          .append('<a href="javascript:void(0);">' + item.label + '</a>')
          .appendTo($('#wrapper'));
          break;



          ;


          check this fiddle






          share|improve this answer













          very simple you just need to set an attribute to div like below



          <div class="element" style="background-color:green; width:30%;"></div>


          your code should be something like this.



          var availableTags = [
          'DHA 11 Rahbar',
          'DHA City',
          'DHA Defence',
          'Dhalloke',
          'Dharampura',
          'DHA 11 Rahbar Phase 1',
          'DHA 11 Rahbar Phase 2',
          'DHA 11 Rahbar Phase 2 Extension',
          'DHA 9 Town',
          'DHA Phase 1',
          'DHA Phase 10',
          'DHA Phase 10',
          'DHA Phase 11 - Halloki Gardens',
          'DHA Phase 2',
          'DHA Phase 3',
          'DHA Phase 4',
          'DHA Phase 5',
          'DHA Phase 6',
          'DHA Phase 7',
          'DHA Phase 8',
          'DHA Phase 9 Prism',
          'DHA - EME Cottages'

          ];

          $('#tags').autocomplete(

          source: availableTags,
          search: function(event, ui)
          $('#wrapper').empty();
          ,
          select: function(e, ui)
          $(this).val(ui.item.name);
          return false;
          ,
          )

          .data('autocomplete')._renderItem = function(ul, item)
          matchFound = 0;
          var cls = this.options.highlightClass;
          var inp = $("#tags").val().split(" ");
          var template = "<span class='" + cls + "'>$1</span>";
          var items = item.label.split(" ");
          for (var i = 0; i < items.length; i++)
          for (var j = 0; j < inp.length; j++)
          index = items[i].toUpperCase().indexOf(inp[j].toUpperCase());
          if (index > -1)
          matchFound += 1;


          if (matchFound == inp.length)
          return $('<div class="element" style="background-color:green; width:30%;"></div>')
          .data('item.autocomplete', item.label)
          .append('<a href="javascript:void(0);">' + item.label + '</a>')
          .appendTo($('#wrapper'));
          break;



          ;


          check this fiddle







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 9:48









          Negi RoxNegi Rox

          1,8861511




          1,8861511












          • No what i mean to highlight just matched word,not entire list.Have a look at this image ibb.co/kCTYSq

            – Khirad Banu
            Nov 13 '18 at 9:55












          • just matched... its only renderd that items which is matched.

            – Negi Rox
            Nov 13 '18 at 9:57


















          • No what i mean to highlight just matched word,not entire list.Have a look at this image ibb.co/kCTYSq

            – Khirad Banu
            Nov 13 '18 at 9:55












          • just matched... its only renderd that items which is matched.

            – Negi Rox
            Nov 13 '18 at 9:57

















          No what i mean to highlight just matched word,not entire list.Have a look at this image ibb.co/kCTYSq

          – Khirad Banu
          Nov 13 '18 at 9:55






          No what i mean to highlight just matched word,not entire list.Have a look at this image ibb.co/kCTYSq

          – Khirad Banu
          Nov 13 '18 at 9:55














          just matched... its only renderd that items which is matched.

          – Negi Rox
          Nov 13 '18 at 9:57






          just matched... its only renderd that items which is matched.

          – Negi Rox
          Nov 13 '18 at 9:57


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278081%2fhow-to-highlight-jquery-autocomplete-matched-word-only%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Crossroads (UK TV series)

          ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế