tags:

views:

75

answers:

3

So, I've got a form and within that form is a <select>. Based on what that select is(what <option> is selected), I want the contents of the form to change on the fly(i.e. before the user clicks anything else).

For example, if the user selects the Photo Upload option, a file upload box will appear, and if they select Text Entry, a text box will appear in place of that file upload box.

Thanks.

A: 

Use the onchange event to trigger the file upload (or whatever).

jldupont
Yes, I know that I should be using the `onchange` event, I just don't know how to make the JS function do what I want.
deftonix
A: 
function swapDiv( index ) {
    var html = '';
    switch( index ) {
       ... // assign html var based on index;
    }
    div.innerHTML = html; // assign div html to html var
}
select.onchange = function() {
    swapDiv( this.selectedIndex );
}
Jacob Relkin
Sorry for being totally clueless on javascript, but I can't get this working. What would the entire HTML document look like? Thanks a million.
deftonix
A: 

this using jquery:

$(function ()
{
    var shown = $($('#my-select').val());

    $('#my-select').change(function ()
    {
        shown.hide();
        shown = this.form.find($(this).val()).show();
    });
});

expects such html:

<style>
textarea, input { display: none; }
</style>
<form id="my-form">
  <textarea id="text-entry"></textarea>
  <input type="file" id="file-upload">
  <select id="my-select">
    <option value="#text-entry">Text Entry</option>
    <option value="#file-upload">File Upload</option>
  </select>
</form>
just somebody