Feivi99
asked on
javascript: highlighting div with arrow keys
Hello,
I have an input which, upon entry of letters, shows a div with input suggestions. Currently the suggestions get highlighted upon mouseover, and the input value becomes the suggestion value when clicking on the suggestion.
How can I have the suggestion be highlighted and populate the input through the user clicking the up and down arrows?
Here's my code:
<html>
<head>
<style type="text/css">
.suggmain
{
display:none;
width:489px;
background-color:White;
position:relative;
padding:1px;
z-index:99999;
border-color:#A0522D;
border-width:2px;
border-style:solid;
}
.suggclass
{
color:#A0522D;
font-size:15px;
font-weight:bold;
margin-top:10px;
}
.suggres
{
height:25px;
}
</style>
</head>
<body>
<div style="position:absolute;t op:80px;wi dth:495px; left:50%; margin-left:-246px;">
<div style="height:40px; background-color:white;wid th:491px;
border-style:solid; border-width:2px; border-color:#A0522D">
<input name="s" id="q" autocomplete="off" type="text" onkeyup="obj.pingSearch(); " style="position:absolute;t op:5px;wid th:400px;l eft:5px;he ight:30px;
font-size:x-large; font-weight:bold;color:#A0 522D; border-color:white;border- style:none ;border-wi dth:0px" onkeypress="var k=event.keyCode||event.whi ch;if(k==1 3){search( )}" />
</div>
<div id="suggbox" class="suggmain" ></div>
</div>
<script type="text/javascript">
function blursugg()
{
document.getElementById('s uggbox').s tyle.displ ay="none";
document.getElementById('s uggbox').i nnerHTML=" ";
}
function sugghil(t)
{
t.style.backgroundColor="# FFE4B5";
}
function sugglol(t)
{
t.style.backgroundColor="# FFFFFF";
}
function choosesugg(t)
{
if(document.all){
var sugv=t.innerText;
} else{
var sugv=t.textContent;
}
document.getElementById("q ").value=s ugv;
document.getElementById('s uggbox').s tyle.displ ay="none";
}
var google = {
ac: {
h: function(queryTerm) {
/* Do something with the queryTerm and/or the suggestions Array */
ln=queryTerm[1].length;
if (document.getElementById(" q").value! ==" "){document.getElementById ('suggbox' ).style.di splay="blo ck";}
else{document.getElementBy Id('suggbo x').style. display="n one";}
document.getElementById('s uggbox').i nnerHTML=" ";
var objr='<div class="suggclass">';
var suggItem='<div class="suggres" onmouseover="sugghil(this) ;" onmouseout="sugglol(this); " onclick="choosesugg(this); ">{res}</d iv>'
for (var i = 0; i < ln; i++)
{
objr+=suggItem.replace('{r es}',query Term[1][i] [0]);
}
objr+='</div>';//alert(obj r);
document.getElementById('s uggbox').i nnerHTML += objr;
}
}
};
var obj =
{
pingSearch : function()
{
var queval=document.getElement ById("q"). value;
if (queval)
{
obj.s = document.createElement('sc ript');
obj.s.type ='text/javascript';
obj.s.charset ='utf-8';
obj.s.src = 'http://suggestqueries.google.com/complete/search?hl=en&q=' + queval;
document.getElementsByTagN ame('head' )[0].appen dChild(obj .s);
}
}
};
</script>
</body>
</html>
I have an input which, upon entry of letters, shows a div with input suggestions. Currently the suggestions get highlighted upon mouseover, and the input value becomes the suggestion value when clicking on the suggestion.
How can I have the suggestion be highlighted and populate the input through the user clicking the up and down arrows?
Here's my code:
<html>
<head>
<style type="text/css">
.suggmain
{
display:none;
width:489px;
background-color:White;
position:relative;
padding:1px;
z-index:99999;
border-color:#A0522D;
border-width:2px;
border-style:solid;
}
.suggclass
{
color:#A0522D;
font-size:15px;
font-weight:bold;
margin-top:10px;
}
.suggres
{
height:25px;
}
</style>
</head>
<body>
<div style="position:absolute;t
<div style="height:40px; background-color:white;wid
border-style:solid; border-width:2px; border-color:#A0522D">
<input name="s" id="q" autocomplete="off" type="text" onkeyup="obj.pingSearch();
font-size:x-large; font-weight:bold;color:#A0
</div>
<div id="suggbox" class="suggmain" ></div>
</div>
<script type="text/javascript">
function blursugg()
{
document.getElementById('s
document.getElementById('s
}
function sugghil(t)
{
t.style.backgroundColor="#
}
function sugglol(t)
{
t.style.backgroundColor="#
}
function choosesugg(t)
{
if(document.all){
var sugv=t.innerText;
} else{
var sugv=t.textContent;
}
document.getElementById("q
document.getElementById('s
}
var google = {
ac: {
h: function(queryTerm) {
/* Do something with the queryTerm and/or the suggestions Array */
ln=queryTerm[1].length;
if (document.getElementById("
else{document.getElementBy
document.getElementById('s
var objr='<div class="suggclass">';
var suggItem='<div class="suggres" onmouseover="sugghil(this)
for (var i = 0; i < ln; i++)
{
objr+=suggItem.replace('{r
}
objr+='</div>';//alert(obj
document.getElementById('s
}
}
};
var obj =
{
pingSearch : function()
{
var queval=document.getElement
if (queval)
{
obj.s = document.createElement('sc
obj.s.type ='text/javascript';
obj.s.charset ='utf-8';
obj.s.src = 'http://suggestqueries.google.com/complete/search?hl=en&q=' + queval;
document.getElementsByTagN
}
}
};
</script>
</body>
</html>
ok this is a bit messy but it works
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
mstrelan,
I didn't think it was messy, I thought it was great :-) !
One more detail, though: can this be modified so that the user can keep his finger on the button to get through the results quicker? RIght now it only goes one by one.
I didn't think it was messy, I thought it was great :-) !
One more detail, though: can this be modified so that the user can keep his finger on the button to get through the results quicker? RIght now it only goes one by one.
ASKER
In addition, the "suggbox: is now not hiding when choosing a suggestion.
Key presses: What browser / operating system? In Firefox and Chrome on Ubuntu it is working fine for me. There is often a repeat delay set in keyboard properties in the operating system...
Suggbox not hiding: In the function choosesugg(t, hide) change the hide = false to hide = true
Suggbox not hiding: In the function choosesugg(t, hide) change the hide = false to hide = true
ASKER
mstrelan,
I realized the problem was that we were using "onkeyup", so obviously holding the button down wouldnt do anything. It's actually a little perplexing that Ubuntu IS allowing it to happen.
Anyway, I divided your code into two functions, with your code being called with a "onkeydown", and it's works great. Thanks!
I realized the problem was that we were using "onkeyup", so obviously holding the button down wouldnt do anything. It's actually a little perplexing that Ubuntu IS allowing it to happen.
Anyway, I divided your code into two functions, with your code being called with a "onkeydown", and it's works great. Thanks!