Skip to content

Instantly share code, notes, and snippets.

@piotrze
Created July 1, 2018 14:41
Show Gist options
  • Save piotrze/aa4aeff7702065aaeaf24364c9ba9f22 to your computer and use it in GitHub Desktop.
Save piotrze/aa4aeff7702065aaeaf24364c9ba9f22 to your computer and use it in GitHub Desktop.
Input that will show matched query
<!DOCTYPE html>
<html lang="en">
<head>
<style>
div{
position:relative;
}
input, span{
top:0;
position:absolute;
width:120px;
}
span{
top:2px;
left:2px;
z-index:1;
overflow:hidden;
}
input{
background:transparent;
z-index:2;
}
</style>
</head>
<body>
<div>
<input onkeyup="fetchResults(this)" />
<span id="styled-input"></span>
</div>
<script>
function fetchResults(input){
ajaxRequest(input.value, function(response){
var query = response.query;
styleInput(input, query.matched, query.non_matched);
});
}
function ajaxRequest(query, callback){
var responseFromServer = {
query: {
matched: query.substr(0,3),
non_matched: query.substr(3)
}
};
callback(responseFromServer);
}
function styleInput(input, matched, nonMatched){
input.style.color = 'transparent';
var span = document.getElementById('styled-input');
span.innerHTML = '<b>' + matched + '</b>' + nonMatched;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment