How to highlight Jquery Autocomplete matched word only?
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
add a comment |
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
add a comment |
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
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
jquery jquery-ui-autocomplete
edited Nov 13 '18 at 12:34
Saurabh Solanki
1,365623
1,365623
asked Nov 13 '18 at 9:44
Khirad BanuKhirad Banu
286416
286416
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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;
}
1
Thank you,this is what i was looking far
– Khirad Banu
Nov 14 '18 at 6:26
add a comment |
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
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
add a comment |
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
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
}
1
Thank you,this is what i was looking far
– Khirad Banu
Nov 14 '18 at 6:26
add a comment |
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;
}
1
Thank you,this is what i was looking far
– Khirad Banu
Nov 14 '18 at 6:26
add a comment |
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;
}
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;
}
answered Nov 13 '18 at 16:28
dganencodganenco
1977
1977
1
Thank you,this is what i was looking far
– Khirad Banu
Nov 14 '18 at 6:26
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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>
edited Nov 13 '18 at 10:28
answered Nov 13 '18 at 10:08
Sumesh TGSumesh TG
2,0721722
2,0721722
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
answered Nov 13 '18 at 9:48
Negi RoxNegi Rox
1,7251511
1,7251511
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
add a comment |
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
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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