Created
July 1, 2018 14:41
-
-
Save piotrze/aa4aeff7702065aaeaf24364c9ba9f22 to your computer and use it in GitHub Desktop.
Input that will show matched query
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 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