I am using this jQuery plugin MagicSearch
How can I programatically select a value (by ID for example)?
This is what I have. After a careful analysis of their callback functions, I have devised a method to extract the ID and Name from my search list.
Please find the code to my attempt:
<script>
var idList="";
var nameList = "";
$(function () {
var dataSource = "[{\"id\":1,\"name\":\"test\"},
{\"id\":2,\"name\":\"example\"},{\"id\":3,\"name\":\"image\"},
{\"id\":4,\"name\":\"election\"},{\"id\":5,\"name\":\"IPL\"},
{\"id\":6,\"name\":\"Fashion\"},{\"id\":7,\"name\":\"Movies\"},
{\"id\":8,\"name\":\"Pollution\"},{\"id\":9,\"name\":\"Modi\"},
{\"id\":10,\"name\":\"Rahul\"},{\"id\":11,\"name\":\"Cricket\"},
{\"id\":12,\"name\":\"Market\"},{\"id\":13,\"name\":\"TestKeyword\"},
{\"id\":14,\"name\":\"testkeyword1\"},{\"id\":15,\"name\":\"testkeyword2\"},
{\"id\":16,\"name\":\"testkeyword3\"}]";
$('#basic').magicsearch({
dataSource: dataSource,
fields: ['name'],
id: 'id',
format: '%name%',
hidden: true,
multiple: true,
focusShow: true,
isClear: true,
multiField: 'name',
multiStyle: {
space: 5,
width: 200
},
success: function ($input, data) {
idList = idList + data.id + ',';
nameList = nameList + data.name + ',';
// alert(data.id);
return true;
},
afterDelete: function ($input, data) {
idList = idList.replace(data.id + ',', "");
nameList = nameList.replace(data.name + ',',"");
//alert(data.id);
return true;
}
});
});
Link to the fiddle: https://jsfiddle.net/yn0cmgjt/5/
Excuse me for the bad interface. I had a difficult time getting the CDN link to this plugin and somehow it is not rendering the correct way. But play around with this and I hope it will suffice to your needs.
The two lists (idList and nameList) that I have created are the id and name for each element concatenated in a single comma separated string. Once you have this string, you can extract out the required information.