Created
August 2, 2017 18:32
-
-
Save afsinka/401dcd63d6a49f5449d3939a14f3ccdd to your computer and use it in GitHub Desktop.
Filter in Select Option List With Textbox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script> | |
<title>Title</title> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
$("#searchBox").keyup(function() { | |
searchInList(); | |
}); | |
}); | |
var searchInList = function() { | |
var myOptions = document.getElementById($('#myOptions').get(0).id); | |
var searchBox = document.getElementById($('#searchBox').get(0).id); | |
$.each(myOptions, function(index, item) { | |
$(item).hide(); | |
var writtenValue = item.innerText.toUpperCase(); | |
var searchedValue = searchBox.value.toUpperCase(); | |
if (writtenValue == "" || searchedValue == "" || writtenValue.indexOf(searchedValue) >= 0) { | |
$(item).show(); | |
} | |
}); | |
}; | |
</script> | |
<input type="text" id="searchBox"> | |
<br /> | |
<select name="myOptionsName" id="myOptions" multiple style="width: 200px;"> | |
<option value="VOLVO">VOLVO</option> | |
<option value="SAAB">SAAB</option> | |
<option value="MERCEDES">MERCEDES</option> | |
<option value="AUDI">AUDI</option> | |
</select> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment