Jump to content

Combo Box


adam84

Recommended Posts

Here is a few function that will help you select any option based on the value, text or index

<script language="javascript">

// select an option based on value
function selSelectValue(sel, val){
var op = sel.childNodes;
var selIndex = 0;
for(i=0; i<op.length; i++){
	if (op[i].tagName == "OPTION"){
		if (op[i].value == val) {
			sel.selectedIndex = selIndex;
			i = op.length+1;
		}
		selIndex++;
	}
}
}

// select an option based on text
function selSelectText(sel, val){
var op = sel.childNodes;
var selIndex = 0;
for(i=0; i<op.length; i++){
	if (op[i].tagName == "OPTION"){
		if (op[i].innerHTML == val) {
			sel.selectedIndex = selIndex;
			i = op.length+1;
		}
		selIndex++;
	}
}
}

// select an option based on index
function selSelectIndex(sel, val){
sel.selectedIndex = val;
}
</script>
<select id="sel" name="sel">
<option VALUE="value1">Hotmail</option>
<option VALUE="value2">Gmail</option>
<option VALUE="value3">Yahoo</option>
</select>	
<br />
Text:<br />
<input type="text" id="val" />
<br /><br />
<button onclick="selSelectValue(document.getElementById('sel'), document.getElementById('val').value);">Select by Value</button> enter value1, value2 or value3<br />
<button onclick="selSelectText(document.getElementById('sel'), document.getElementById('val').value);">Select by Text</button> enter Hotmail, Gmail, Yahoo<br />
<button onclick="selSelectIndex(document.getElementById('sel'), document.getElementById('val').value);">Select by Index</button> enter 0, 1, 2

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.