/    Sign up×
Community /Pin to ProfileBookmark

Simulating a SELECT dropdown menu

Considering the difficulty surrounding properly stylizing a dropdown box (not to mention having a consistent cross-platform appearance) I’m trying to simulate a dropdown menu using javascript and hidden tables.

With the understanding that older browsers or users with javascript disabled wouldn’t be able to use it, the actual design has been pretty straightforward. Stylizing and javascript functions will be more appropriately handled in the final design — this is just a quick POC.

The dropdown itself is a series of nested tables. The outer table has two rows and one column: the first row features the onClick events in the TD and within the cell body is a span defined so that the value within can be changed by the dropdown. The second row contains the nested table that is positioned absolutely and with the visibilty attribute set to hidden. This table is a multiple row, single column that catches onClick events at the TD element to change the upper cell, then rehides the nested table.

So the code for the dropdown looks something like this:

[code]
<table border=’1′ cellpadding=’0′ cellspacing=’0′ width=’100px’>
<tr>
<td onClick=’document.getElementById(“dropdown1″).style.visibility=”visible”;’>
<span id=’target1′>Test 1</span>
</td>
</tr>
<tr>
<td>
<table border=’1′ id=’dropdown1′ style=’position:absolute;visibility:hidden’>
<tr>
<td onClick=’document.getElementById(“target1″).innerHTML=”Test 1”;document.getElementById(“dropdown1″).style.visibility=”hidden”;’>Test 1
</td>
</tr>
<tr>
<td onClick=’document.getElementById(“target1″).innerHTML=”Test 2”;document.getElementById(“dropdown1″).style.visibility=”hidden”;’>Test 2
</td>
</tr>
<tr>
<td onClick=’document.getElementById(“target1″).innerHTML=”Test 3”;document.getElementById(“dropdown1″).style.visibility=”hidden”;’>Test 3
</td>
</tr>
</table>
</td>
</tr>
</table>
[/code]

And appears something like this: [ATTACH]12974[/ATTACH]

Now, two of the three important features are implemented: clicking on the top cell creates the dropdown view, and clicking on an element changes the top cell and disappears the rest of the options.

The issue I’m having is cancelling the menu when the user changes changes focus instead of clicking an option. AFAIK, There’s no (reliable) onBlur event for table or block elements. Setting an onClick event for the body or an encapsulating DIV seems to just cancel out the onClick handlers for the table.

Any suggestions as to how to handle this situation?

[upl-file uuid=34c8d0e1-c354-4ed9-94db-6d1fc5b2a418 size=1kB]dropdown1.png[/upl-file]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@AlzheimersauthorAug 13.2009 — So perhaps the title is misleading -- The base of it is that I'm looking for a way to simulate an OnBlur event for a block element. Any ideas?
Copy linkTweet thisAlerts:
@AlzheimersauthorAug 14.2009 — After some more experimentation, I found that I can get an onClick event in the body to simulate the onBlur effect as I wanted, by adding a setTimeout to the display code. With a bit of naming trickery, I can generalize the hiding code in a function and remove them from table elements completely.

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script type='text/javascript'&gt;
function hidedrops()
{
var x = document.getElementsByName("dropdown");

for (var y = 0; y &lt; x.length; y++)
x[y].style.visibility = "hidden";
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onClick='hidedrops()'&gt;
&lt;table border='1' cellpadding='0' cellspacing='0' width='100px'&gt;
&lt;tr&gt;
&lt;td onClick='setTimeout("document.getElementById("d1").style.visibility="visible"",10)'&gt;
&lt;span id='target1'&gt;Test 1&lt;/span&gt;
&lt;/td&gt;
&lt;td onClick='setTimeout("document.getElementById("d2").style.visibility="visible"",10)'&gt;
&lt;span id='target2'&gt;Test A&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;table border='1' name='dropdown' id='d1' style='position:absolute;visibility:hidden;' width='100px'&gt;
&lt;tr&gt;
&lt;td onClick='document.getElementById("target1").innerHTML="Test 1";'&gt;Test 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td onClick='document.getElementById("target1").innerHTML="Test 2";'&gt;Test 2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td onClick='document.getElementById("target1").innerHTML="Test 3";'&gt;Test 3&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;table border='1' name='dropdown' id='d2' style='position:absolute;visibility:hidden;' width='100px'&gt;
&lt;tr&gt;
&lt;td onClick='document.getElementById("target2").innerHTML="Test A";'&gt;Test A&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td onClick='document.getElementById("target2").innerHTML="Test B";'&gt;Test B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td onClick='document.getElementById("target2").innerHTML="Test C";'&gt;Test C&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;


By changing the behavior of the dropdown onClick handler, you can even turn this into a nice simple dropdown menu. I'm sure it can be made even more elegant by moving the inline styling to CSS; that exercise is left to the reader.
×

Success!

Help @Alzheimers 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...