javascript combo box

R

rodchar

hey all,
how do you pass what is selected in a combo box as a parameter to a
javascript function call.

i know you can do this
onclick=test(this)

thanks,
rodchar
 
D

David R. Longnecker

rodchar-

Here's an example using an HTML SELECT and an .net DropDownList control.
The JavaScript is the same for both. I'd recommend the onChange event,
not onClick, since onClick fires when the control itself is simply clicked
and onChange waits until you CHANGE the control (which, in this case, is
it's selected index value).

html:

<form id="form1" runat="server">
<div>
<select id="SelectList" onchange="javascript:test(this)">
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
<option value="Option 5">Option 5</option>
</select>

<asp:DropDownList runat="server" ID="SelectListControl" onchange="javascript:test(this)">
<asp:ListItem Value="Option 1" Text="Option 1" />
<asp:ListItem Value="Option 2" Text="Option 2" />
<asp:ListItem Value="Option 3" Text="Option 3" />
<asp:ListItem Value="Option 4" Text="Option 4" />
<asp:ListItem Value="Option 5" Text="Option 5" />
</asp:DropDownList>

<div id="output"></div>
</div>
</form>

JavaScript:

<script type="text/javascript">
function test(o)
{
var selectedValue = o.value;
var output = document.getElementById('output');
if (typeof output.textContent != 'undefined')
output.textContent = selectedValue.toString(); // Firefox
else
output.innerText = selectedValue.toString(); // IE
return true;
}
</script>

HTH.

-dl
 
B

bruce barker

as the browser does not have a combobox,you are looking at an custom
control. you will need to review the docs (or source code) for the
control you pick.

if you mean the select (dropdown in asp.net) it simple

function test(e)
{
alert(e.value);
}


-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top