how to get the selected combo values in a text box using on change event
getting combo values
Collapse
X
-
Tags: None
-
hi gits,
here is my code
[HTML]<html>
<head>
<script type="text/javascript">
function moveNumbers()
{
var no=document.get ElementById("no ")
var option=no.optio ns[no.selectedInde x].text
var txt=document.ge tElementById("r esult").value
txt=txt + option
document.getEle mentById("resul t").value=tx t
}
</script>
</head>
<body>
<form>
Select numbers:<br />
<select id="no" onchange="moveN umbers()" multiple="3">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
</select>
<input type="text" id="result" size="20">
</form>
</body>[/HTML]
for the above code if suppose i select three values i need to get that values in that text box with comma separated with onchange eventComment
-
hi ...
use the following function, and have a look how it works. selectedIndex only returns the first index of the selected option, so we have to loop and ask for the selected options:
[CODE=javascript]
function moveNumbers() {
var no_node = document.getEle mentById("no");
var no_node_options = no_node.childNo des;
var result_node = document.getEle mentById("resul t");
var result_text = '';
for (var i = 0; i < no_node_options .length; i++) {
var opt = no_node_options[i];
if (opt.selected) {
result_text += opt.text + ', ';
}
}
// remove last comma through matching an regEx
result_text = result_text.rep lace(/(, )$/, '');
result_node.val ue = result_text;
}
[/CODE]
and some additional hints ;)
- use code-tags when posting code here in the forum
- always use a semicolon to terminate every line of code in js
kind regardsComment
Comment