Matthew
Matthew

Reputation: 195

dropdown menu to create text field

I have spent about an hour or two trying to find out how to use JavaScript to create a text field when a certain value is selected from a drop down list.

Here is the code for the drop down menu if that's of any help. I'd have attempted to write the code to do this myself, but I am still very unfamiliar with how to write JavaScript code

<select name="claim">
    <option value="">Select a Claim</option>
    <option value="Insurance">Insurance</option> // this option I want when selected to create a text field
    <option value="Warranty">Warranty</option>
</select>

Upvotes: 0

Views: 1470

Answers (1)

COLD TOLD
COLD TOLD

Reputation: 13579

try this hope it helps

   <div id="area">
         <select id="claim" name="claim">
            <option value="">Select a Claim</option>
        <option value="Insurance">Insurance</option>  
        <option value="Warranty">Warranty</option>
        </select>
        </div>

and script

   $(document).ready(function(){
          $("#claim").change(function(){       
             $("#area").find(".field").remove(); 
             //or
               $('#area').remove('.field');
          if( $(this).val()=="Insurance")
             {
        $("#area").append("<input class='field' type='text' />");

             }
          });

});

example http://jsfiddle.net/cqjJy/

Upvotes: 2

Related Questions