/    Sign up×
Community /Pin to ProfileBookmark

styling up my auto suggestion menu

I have an auto suggestion box for my form. When someone types in a few letters in an input field, the script brings up a list of choices that relate to what was typed in. So for example, if the user typed in ‘Sa’, a list of names beginning with ‘Sa’ will pop up, and the user can select from one.

The problem is that the list of choices can be very long, and it pushes the rest of my site content down. I want the box to have its own layer if you know what I mean so that it overlaps the rest of my content. Also, because the list can be very long, I want there to be a fixed height for the box with a scrollbar, so the user will scroll through the list of choices.

[code]
#inputtext {
font-family: verdana;
color: #3d3d3d;
}

#suggestions ul {
margin-top: 0px;
margin-left: 70px;
}

#suggestions li {
width: 200px;
background-color: #e0eefa;
list-style-type: none;
}

#suggestions li a:hover {
background-color: #3d3d3d;
color: #FFF;
display:block;
}

#suggestions a:link, #suggestions a:active, #suggestions a:visited {
text-decoration: none;
color: #6EADE7;
}
[/code]

HTML

[code]
<form action=”functions.php” method=”post”>
<input name=”action” type=”hidden” value=”dexsearch” />
<p>Search Pok&eacute;dex: <input type=”text” name=”pokemon” id=”inputtext” onkeyup=”suggest(this.value)” /> <input type=”submit” name=”submit” value=”Search” class=”submit” /></p>
</form>
<div id=”suggestions”> </div>
[/code]

Now I’ve tried something like:

#suggestions li {
width: 200px;
background-color: #e0eefa;
list-style-type: none;
[b]overflow: auto;
height: 200px;[/b]

}

but that didn’t work. All it did was create 200px height in between each suggestion and not the whole box in general. I don’t know if it’s needed or not, but here’s my javascript just in case.

[code]
var xmlHttp;

function suggest(str) {

if (str.length==0) {
document.getElementById(“suggestions”).innerHTML=””;
return;
}

xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert (“Browser does not support HTTP Request”);
return;
}

var url=”includes/autosuggest.php”;
url=url+”?query=”+str;
url=url+”&sid=”+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open(“GET”,url,true);
xmlHttp.send(null);
}

function correct(str) {

var elem = document.getElementById(“inputtext”);
elem.value=trim(str);
document.getElementById(“suggestions”).style.display = ‘none’;

}

function stateChanged() {
if (xmlHttp.readyState==4 || xmlHttp.readyState==”complete”){
var split = xmlHttp.responseText.split(“,”);
var list = “”;

if(split[0] != “”) {
for(var i = 0; i < split.length; i++) {
list+=”<li><a href=”#” onclick=”javascript: correct(‘”+split[i]+”‘);”>”+split[i]+”</a></li>”;
}

if(list != “”) {
list +=”</ul>”;
list = “<ul>” + list;
}
}

document.getElementById(“suggestions”).innerHTML=list;
document.getElementById(“suggestions”).style.display = ‘block’;
}
}

function GetXmlHttpObject() {
var xmlHttp=null;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
// Internet Explorer
try {
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}catch (e){
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}

function trim(str) {
if (str != null) {
var i;
for (i=0; i<str.length; i++) {
if (str.charAt(i)!=” “) {
str=str.substring(i,str.length);
break;
}
}

for (i=str.length-1; i>=0; i–) {
if (str.charAt(i)!=” “) {
str=str.substring(0,i+1);
break;
}
}

if (str.charAt(0)==” “) {
return “”;
} else {
return str;
}
}

return null;
}
[/code]

So can anyone help?

to post a comment
CSS

3 Comments(s)

Copy linkTweet thisAlerts:
@ray326Aug 23.2008 — The list will have to be positioned absolute to overlay its surroundings.
Copy linkTweet thisAlerts:
@Static42authorAug 24.2008 — Thank you. I did that and it worked.

New problem. Take a look at this pic:

[img]http://img.photobucket.com/albums/v91/SableyeRULES/dropdown.png[/img]

The text and the blue background doesn't extend all the way to the left to where the border is suppose to be. How can I make it do that?

Also, is there a way to get a dynamic height for this dropdown menu? I want the max height to be 200px, but if there is only one suggestion listed, then it obviously will be less than 200px. I want the height to remain consistent with the blue background. As you can see from the screenshot, the height is static and it doesn't look too good.

I've updated my CSS a bit, so here it is:

<i>
</i>#inputtext {
font-family: verdana;
color: #3d3d3d;
}

#suggestions {
position: absolute;
width: 158px;
left: 130px;
top: 22px;
}

#suggestions ul {
margin-top: 0px;
overflow: auto;
height: 200px;
border: 1px solid #000;
}

#suggestions li {
background-color: #e0eefa;
left: 0;
width:200px;
list-style-type: none;
}


Thanks to anyone who can help.
Copy linkTweet thisAlerts:
@ray326Aug 24.2008 — Try the background on the ul rather than the li.
×

Success!

Help @Static42 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...