Dynamic Drop Down
Anyone knows how I could dynamically add selected index to a select option.
Found this online but couldn't figure out how to add selected index to it.
function addOption(selectbox, value, text)
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
Thanks for your help
option has no selected index, the select element has this property.
use [code]YOUR CODE GOES HERE[/code] or burn in Hell
See this page Change Select element content in the fly and use the constructor new Option(text, value, defaultSelected, selected) !
Thanks 007Julien, will check that out
if the option already exists and you just want it to be selected, it would be something like this (if "sel" were the id of your select and you wanted the 3rd option selected):
you can also give your options id's and select them that way:
and all sorts of other stuff, depending on what it is that you actually want to do
<input type="button" onclick="changeSel()" value ="change it!"/>
Thanks guys, got it working perfectly but my problem is how to dynamically selected the a default value, I have tried using ternary operator inside the constructor but it didnt work.
document.frmadd.subcat.options= new Option("Weddings", "8", false, (frmadd.subcat.value == '8')?true:false)
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)