views:

57

answers:

5

Hi

I want to have a drop down menu where a user can select from several predefined options but i also want to give him the option of inserting a user specific value, i was thinking having one of the options as "user specific" which will in turn allow the user to insert a user specif entry in a text box that appears or is editable when user selects the "user specific" option. any one have any ideas how i can implement this is HTML and Javascript?

thank you

+1  A: 

Jukka wrote an article on how to create a combobox-like effect. I've only skimmed it, but it looks accessible and his content is usually of very high quality.

David Dorward
+3  A: 

Attach an onchange event to the select and when he selects "Custom" or something like that, make some input visible below the select. This input should be initially hidden (css display:none). When the user selects "custom" make it display:inline.

nc3b
+1  A: 

You can go about like:

<select id="myid" onchange="doIt(this);">
  optons...
</select>

<script type="text/javascript">
function doIt(field) {
if (field.value === 'user specific')
{
  var tbox = document.createElement('input');
  tbox.setAttribute('type', 'text');
  document.form_name.appendChild(tbox);
}
}
</script>
Sarfraz
+1  A: 

See here: http://jsfiddle.net/EscRV/

html

<select id="mySelect">
   <option>value 1</option>
   <option value="[new]">new</option>
</select>

js

// get a cross-browser function for adding events, place this in [global] or somewhere you can access it
var on = (function(){
    if (window.addEventListener) {
        return function(target, type, listener){
            target.addEventListener(type, listener, false);
        };
    }
    else {
        return function(object, sEvent, fpNotify){
            object.attachEvent("on" + sEvent, fpNotify);
        };
    }
}());

var mySelect = document.getElementById("mySelect");    
on(mySelect, "change", function(){
    if (this.value === "[new]") {
        var newOptionName = prompt("input new option");
        if (newOptionName){
            mySelect.options[mySelect.options.length] = new Option(newOptionName);
            this.value= newOptionName;
        }
    }
}); ​
Sean Kinsey
A: 

Following code will help to solve your problem.

<html>
<head></head>
<body>

<select id="sbox">
<option value="1" onclick="hideUserOption();">First option</option>
<option value="2" onclick="hideUserOption();">second option</option>
<option value="3" onclick="showUserOption();">User Specific</option>
</select>
<div id="userOptions" style="display:none">
<input type="text" />
<input type="submit" value="Save"/>
</div>
<script type="text/javascript">

    function showUserOption() {
        var userOptions = document.getElementById("userOptions");
        userOptions.style.display = "block";
    }

    function hideUserOption() {
        var userOptions = document.getElementById("userOptions");
        userOptions.style.display = "none";
    }


</script>
</body>



</html>
Adeel