Forum Moderators: open

Message Too Old, No Replies

Open (reveal) <select> options

How to do it through javascript?

         

vincevincevince

7:48 am on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you click on a <select> element, a list of options appears. I want to make the list appear via javascript but cannot find any method for this as yet...

How is it done?

Drag_Racer

1:39 pm on May 17, 2007 (gmt 0)

10+ Year Member



I don't know if any method, but you could create a div which looks exactly like it, dynamic population by scanning the options array... use anchors as block elements in the div so you can use :hover to change highlight, etc... onblurr hides it, anchor click sets the selectedIndex of the real select

just a thought...

vincevincevince

11:39 am on May 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the suggestion, but that's the kind of complexity I am trying hard to avoid, besides it being semantically incorrect. Any other takers for this one? Even if the solution only works on one browser....

Bernard Marx

2:23 pm on May 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How's about this dodgy one?

<style type="text/css">
#container{position:relative;height:18px;}
#select1{position:absolute;}

</style>
<script type="text/javascript">
function expandSelect(id, size)
{
var select = document.getElementById(id);
select.size = select.options.length;
}
</script>

<div id="container">
<select size="1" id="select1" onclick="this.size=1">
<option value="">hello
<option value="">abc
<option value="">234
</select>
</div>
<br>
<button onclick="expandSelect('select1')"> Test </button>

[edited by: Bernard_Marx at 2:25 pm (utc) on May 19, 2007]

vincevincevince

3:31 pm on May 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bernard Marx, thanks for that... it's an interesting solution and I think I can work with it! Obviously the GUI doesn't quite match up but I think I can live with that.

daveVk

12:51 am on May 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the click method on the select element?

doodlebee

1:41 pm on May 23, 2007 (gmt 0)

10+ Year Member



Tell you what Vince, I'll share my script if you can tell me how to fix it. :) I was getting ready to post a new thread for this, but maybe giving you a partial answer will help both of us.

I have a script that, when you choose a certain <option> element, more of the form appears. My issue - at first - was that the script relied on inline style set to "display:none;" for the div you wanted to show/hide. This caused end users with javascript turned off to not see the extended form.

I figured out how to get around that by setting the display property for the div to "display:;", but using a body onload event to initially set the div for "display:none;" - this way, end users with javascript turned off would see the entire form (nothing would be hidden), but if you had javascript turned on, then the form would show as collapsed by default (until you clicked the trigger <option> to expand the rest).

Now my issue is this: it's a PHP-based form, and when invalid input is entered, the form is returned with error messages. Problem is, if the end user sent the form fully expanded (filled out everything) and is returned with an error, the form comes back collapsed again. I need to figure out way for the form to check if the input was put in and the form has been returned so it *stays* expanded - but remains collapsed when the page is first loaded.

The script is coded so that, if an error is returned, the field still hold the end user's input (so they don't have to re-enter the whole thing) - so I'm thinking I could use javascript to see if there's input already entered into the first field that shows when it's expanded, and then return the body onload as "display:;" instead of "display:none;". But I don't know how to write such a conditional.

That all being said, here is the code:


showhideID.js
function hide(id) {
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.display == ""){
obj.style.display = "none";
}
}
}

function show(id) {
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.display == "none"){
obj.style.display = "";
}
}
}

The form field (just showing you the section you need to know - cleaned up for ease of seeing what's going on):

<body id="page" onload="hide('services_inquiry');">

...more stuff here that's not necessary...

<div class="form_field">
<?php if ($mailto_error == "1") { echo "<small class=\"error\">You must choose an option for your email type.</small>"; }?>
<span class="fieldname"><span class="required">*</span> Nature of Inquiry:</span>
<select class="form" name="mailto">
<?php if (isset($_POST['mailto'])) echo '<option selected>'.(($_POST['mailto'])).'</option>';?>
<option class="select_option" value="">&nbsp;----------------------------</option>
<option value="general" onclick="hide('services_inquiry');">General Information</option>
<option value="services" onclick="show('services_inquiry');">Inquiry for Services</option>

</select>
</div>

<div id="services_inquiry" style="display:;">

this is the section that expands when the "services_inquiry" option is selected above. By default, this section is hidden when the visitor comes to the page for the first time. The first field I have displayed is for the phone number ($phone).

</div>

Now that I've shared that - if someone can tell me how to write a bit of javascript for the onload event that says something like (I could *so* write this in PHP, I just don't know how to convert it to javascript):


if($phone!= ""){
echo '<body id="page" onload="show('services_inquiry');">';
} else {
echo '<body id="page" onload="hide('services_inquiry');">'
}

Of course, the problem with the above is that, if javascript is turned off, it won't display squat. So if I could figure out some kind of javascript so that the "onload" event is just inserted into the already-exisintg <body> tag if javascript is turned on (but no "onload" is inserted if javascript is off), and then display the "show" or "hide" based on whether or not there's already exisintg input in the $phone field.

Now, is there any possible way to do that?

doodlebee

3:04 pm on May 23, 2007 (gmt 0)

10+ Year Member



Ha - I got it. Of *course* I could do it in PHP. I replaced the <body> tag with this:


<?php if(($_POST['mailto']) == "services" ) {?>
<body id="page" onload="show('services_inquiry');">
<?php } else {?>
<body id="page" onload="hide('services_inquiry');">
<?php }?>

And that solves the problem. I love PHP.

Hope it helps you out Vince!