views:

36

answers:

1

I have a table with a few rows of data. I would like to display a row based on what option is selected on the ddl. how do I do that?

<script type="text/javascript" language="javascript">
     function optionSelected() {
          alert('HELP!!');
     }
</script>
...
<select id="optionSelect" onchange="optionSelected()">
    <option id="1">1</option>
    <option id="2">2</option>
    <option id="3">3</option>
</select>
<br />
<table id="optionList">
    <tr><td id="1">Option 1 Selected</td></tr>
    <tr><td id="2">Option 2 Selected</td></tr>
    <tr><td id="3">Option 3 Selected</td></tr>
</table>
+4  A: 

First I'd apply the handler using javascript rather than inline. Second, you don't say how you know which row goes with which element in the dropdown, so I'll assume it's the numeric value of the option. Note that the rows are counted from zero, whereas your options are numbered from one.

$('#optionSelect').change( function() {
     var val = int.Parse($(this).val(),10) - 1; // calculate row number

     $('#optionList').find('tr').hide() // hide all rows
                     .eq(val) // get the selected row
                     .show(); // and show it
});
tvanfosson
Do you need to define the value in the html, or is 0,1,2,3 the default when they aren't specified? i.e. `<option value='1'>1</option>`
Paul Creasey
@Paul - `val()` will return the text inside the `<option>` tags if there is no explicit `value` attribute.
John Rasch
@tvanfosson - I had intentions of having an id on <option> to coincide with the <tr> (<option id="1">... <tr id="1">)
@user: Note that element ids must be unique. I'd suggest adding the option value as a class on the row and use `hasClass(val)` to filter out all but the chosen row.
tvanfosson