How to delete last character from a input textbox using JavaScript? I make this small task Its function is that when we write a letter in the input, it checks the index number of the value in the queue. And we will choose them all And we will select everyone in the input except the letter we have written.
- Now I want that when I delete a letter, it will be deleted and the getSelectedText function will be run.
- And when I press the control button or any other Esc key. So let's show all the values in the Array.
HTML Code:
<input id="autocomplet" onkeyup="filter(this.value);">
Javascript Code:
<script>
var data = document.querySelector('#autocomplet');
var myArray = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"];
function filter(letter) {
var results = [];
var len = myArray.length;
for (var i = 0; i < len; i++) {
if (myArray[i].indexOf(letter) == 0){
results.push(myArray[i]);
data.value = results;
getSelectedText(data,letter);
}
}
if((event.keyCode == 46) || (event.keyCode == 8)){
data.value='';
}
}
function getSelectedText(data,letter) {
data = data.value;
var datalength = data.length;
var letterlength = letter.length;
autocomplet.setSelectionRange(letterlength,datalength);
}
</script>
Via Active questions tagged javascript - Stack Overflow https://ift.tt/CL3pYB8
Comments
Post a Comment