Forum Moderators: open

Message Too Old, No Replies

Javascript drop-down box

         

adni18

8:57 pm on Dec 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi. I was wondering if there is a way to make a cross-browser fake drop-down menu, so one can control the borders of it. Does anyone know of a site that has this kind of code?

Rambo Tribble

10:26 pm on Dec 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you describe that a bit more? I'm not seeing it. Like, why fake and which borders?

adni18

12:01 am on Dec 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A person cannot control the borders of a <select> box (ie. make them 1px solid red). I know there are javascript-created d.d.b.s, but I don't know where.

adni18

12:38 am on Dec 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was amazed I could actually find this post [webmasterworld.com] on the subject, considering it is so old; I remember reading another similar post here, and someone suggested a url with a script for 'synthetic' drop-down boxes.

Rambo Tribble

12:55 am on Dec 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can I presume you want to make this part of a form? And if so, when it's submitted will it be processed locally or at the server?

adni18

9:54 pm on Dec 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It will be processed at the server, and yes, it will be used in a form.

Rambo Tribble

3:31 am on Dec 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think it should be fairly easy to do. If I get a little time I'll try to work up a demo. Here's what I'm thinking: Use a text input area for the selection box part that is visible. Make it readonly (it could still be modified by the script). Clicking on it will expose a div that has more text inputs as the options, again readonly. Put an event handler on the option text inputs to pass the item's value to a hidden input in another form, hide the div and make the visible "selection box" take the selected value.

The posting to the hidden input in a separate form is to reduce transmission overhead (hence the local/server question). That way you're transmitting only selections, not all the text inputs.

The minor JavaScript tedium will be in making it all work with passed values, so the effect is portable. The layout tedium is going to be getting things to work well in positionable entities and including a failsafe form within noscript tags.

Here's a quick CSS widget for the down arrow (use on a white background):

<div style="height:0;
width:0;
margin:2px auto;
padding:0;
overflow:hidden;
border-left:9px solid #fff; <!-- set to background color -->
border-right:9px solid #fff; <!-- set to background color -->
border-top:7px solid #f00;}">
</div>

adni18

4:53 pm on Dec 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



umm.. the widget you posted doesn't appear in my browser.

Rambo Tribble

11:30 pm on Dec 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try it as a single line, without the comments and the extraneous curly brace.

Rambo Tribble

3:24 pm on Dec 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here it is, c-n-p:

<div style="height:0;
width:0;
margin:2px auto;
padding:0;
overflow:hidden;
border-left:9px solid #fff;
border-right:9px solid #fff;
border-top:7px solid #f00;">
</div>

Rambo Tribble

7:28 am on Dec 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a rough approximation of the idea:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Select substitute demo 12-23-04</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
html,body{height:100%;}
#main{height:100%;width:100%;background:#aaf;}
#widg{height:0;width:0;margin:4px auto;padding:0;overflow:hidden;
border-left:5px solid #ada;border-right:5px solid #ada;border-top:5px solid #f00;}
#widgCont{position:absolute;height:12px;width:1.2em;background:#ada;
border:3px outset;margin:0;padding:0;right:0;text-align:center;}
.selNam{background:transparent;border:0;display:block;position:absolute;cursor:default;}
.selOpt{border:0;display:block;width:100%;cursor:default;}
</style>
<script type="text/javascript">
function optSet(){
var in_puts=document.getElementsByName("selOpt_1");
var in_puts_ln=in_puts.length;
for(var i=0;i<in_puts_ln;i++){
in_puts[i].onmouseover=function(){
this.focus();
this.style.background="#faa";
}
in_puts[i].onmouseout=function(){
this.style.background="";
this.blur();
}
in_puts[i].onclick=function(){
document.getElementById("selNam_1").value=this.value; //set selected value
document.getElementById("submSel_1").value=this.value; //set value in separate submission form
document.getElementById("dropDiv_1").style.visibility="hidden";
this.blur();
}
}
}
</script>
</head>
<body onload="optSet();">
<div id="main">
<form id="submiForm" action=""> <!-- form to submit, using less bandwith -->
<input id="submSel_1" type="hidden" value="" />
</form>
<div>
<form id="selForm" action="">
<div style="background:#fcf;border:4px groove #faa;width:10em;position:relative;height:18px;width:11em;margin:0;padding:0;">
<input type="text" id="selNam_1" class="selNam" onclick="document.getElementById('dropDiv_1').style.visibility='visible';" readonly="readonly" value="Select Name" />
<div id="widgCont">
<div id="widg">
</div>
</div>
</div>
<div id="dropDiv_1"
style="position:relative;width:10em;background:#fff;border-top:8px dashed #daa;border-right:10px ridge #afc;border-bottom:12px double #d88;border-left:16px groove #dbb;visibility:hidden;">
<input type="text" name="selOpt_1" class="selOpt" readonly="readonly" value="First Selection" />
<input type="text" name="selOpt_1" class="selOpt" readonly="readonly" value="Second Selection" />
<input type="text" name="selOpt_1" class="selOpt" readonly="readonly" value="Third Selection" />
</div>
</form>
</div>
</div>
</body>
</html>

adni18

2:30 pm on Dec 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks.