Ad
Javascript IndexOf Array Different Result On Browser
Here is my snippet
var arrays = [
['name1', -10, 100, 1, 'abc', 'xxx'],
['name1', -10, 100, 1, 'abc', 'xxx'],
['name2', -8, 80, 1, 'bbb', 'zzz'],
['name1', -10, 100, 1, 'abc', 'xxx']
];
$('#searchsite').keyup(function() {
var keyword = $(this).val();
var minlength = 3;
var result = [];
if (keyword.length >= minlength) {
for (var i = 0; i < arrays.length; i++) {
if (arrays[i][0].toLowerCase().indexOf(keyword) !== -1) {
result.push('<input type="button" name="button" class="btn btn-danger" value="' + arrays[i][0] + '"><br>');
}
}
resultunique = jQuery.unique(result);
$('#searchresult').html(resultunique);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="searchsite" />
<div id="searchresult"></div>
any reason why getting different result on firefox and chrome
Ad
Answer
Your use of $.unique() is wrong, it works only on dom elements.
Description: Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.
Since you are creating a new input
element markup every time, there are no duplicates.
You can implement the unique logic yourself
var arrays = [
['name1', -10, 100, 1, 'abc', 'xxx'],
['name1', -10, 100, 1, 'abc', 'xxx'],
['name2', -8, 80, 1, 'bbb', 'zzz'],
['name1', -10, 100, 1, 'abc', 'xxx']
];
$('#searchsite').keyup(function() {
var keyword = $(this).val().toLowerCase();
var minlength = 3;
var result = [],
keys = {};
if (keyword.length >= minlength) {
result = $.map(arrays, function(array) {
var name = array[0].toLowerCase();
if (!keys[name]) {
keys[name] = true;
if (name.indexOf(keyword) !== -1) {
return '<input type="button" name="button" class="btn btn-danger" value="' + array[0] + '"><br>';
}
}
})
$('#searchresult').html(result.join(''));
} else {
$('#searchresult').html('');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="searchsite" />
<div id="searchresult"></div>
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad