js implements the keyboard up down left and right keys to select text and display in a text box
- 2020-06-07 03:59:46
- OfStack
This article illustrates js's method of selecting text on the keyboard with the left and right keys and displaying them in a text box. Share to everybody for everybody reference. Specific implementation methods are as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>SimulateUpDownKeySelect.html</title>
<style type="text/css">
#divSelect {border:1px solid red; width:208px !important;width:210px;}
#divSelect ul {width:200px;margin:3px; margin-left:-35px;*margin-left:3px;overflow:hidden}
#divSelect ul li {float:left; list-style-type:none;width:45px;height:14px;line-height:20px;font:14px arial;text-align:center;padding:2px}
#divSelect li:hover {background :green;cursor:pointer}
#txtInput {width:205px;}
</style>
</head>
<body>
<form method="post" action="##">
<input type="text" id="txtInput" value="" autocomplete="off" onkeydown="if(event.keyCode==13)return false;" />
<!--
Prevents the enter key from triggering a form submission
onKeyPress
-->
<div id="divSelect">
</div>
<script type="text/javascript">
var list="<ul>"
list+="<li> Science fiction </li><li> War movies </li><li> Action movies </li><li> Love story </li><li> A drama </li><li> The documentary </li><li> Variety show piece </li><li> Comedy, </li><li> animation </li><li> Inspiring movies </li><li> Horror movies </li><li> Costume dramas </li><li> TV series </li><li> Reading a book </li><li > A novel </li><li> A collection of </li><li> history </li><li> poetry </li><li > prose </li><li> military </li>";
list+="</ul>"
document.getElementById('divSelect').innerHTML=list;
</script>
</form>
<script type="text/javascript">
<!--
function $(sId)
{
return document.getElementById(sId);
}
function clearSelectedOptBgColor(target)
{
if (target.seletedIndex >= 0)
target.options[target.seletedIndex].style.background = "";
}
function setSelectedOptBgColor(target)
{
target.options[target.seletedIndex].style.background = "green";
}
var upKeyCode = 38;
var downKeyCode = 40;
var enterKeyCode = 13;
var oInput = $("txtInput");
oInput.options = $("divSelect").getElementsByTagName("li");
oInput.seletedIndex = -1;
oInput.focus();
//oInput.onKeyPress{}
oInput.onkeyup = function(event){
if (event == undefined)
event = window.event;
switch (event.keyCode)
{
case 37:
clearSelectedOptBgColor(this);
this.seletedIndex--;
if (this.seletedIndex < 0)
this.seletedIndex = this.options.length - 1;
this.value = this.options[this.seletedIndex].innerHTML;
setSelectedOptBgColor(this);
break;
case 38:
clearSelectedOptBgColor(this);
this.seletedIndex= this.seletedIndex-4;
if (this.seletedIndex < 0)
this.seletedIndex = this.options.length - 1;
this.value = this.options[this.seletedIndex].innerHTML;
setSelectedOptBgColor(this);
break;
case 39:
clearSelectedOptBgColor(this);
this.seletedIndex++;
if (this.seletedIndex >= this.options.length)
this.seletedIndex = 0;
this.value = this.options[this.seletedIndex].innerHTML;
setSelectedOptBgColor(this);
break;
case 40:
clearSelectedOptBgColor(this);
this.seletedIndex= this.seletedIndex+4;
if (this.seletedIndex >= this.options.length)
this.seletedIndex = 0;
this.value = this.options[this.seletedIndex].innerHTML;
setSelectedOptBgColor(this);
break;
case enterKeyCode:
this.value = this.options[this.seletedIndex].innerHTML;
//alert('aa')
break;
}
};
oInput.onblur = function(){
clearSelectedOptBgColor(this);
this.seletedIndex = 1;
};
//-->
</script>
</body>
</html>
Hopefully, this article has been helpful in your javascript programming.