I have been using some Javascript to create a text field once an certain value is selected from a drop-down box, but having a limited knowledge in Javascript now that I need to edit it so that a new dropdown option also creates a text field. When I have tried to edit it seems not not work.
Here is the js I’m an trying to use:
$(".claim").change(function() {
$(".area").find(".field").remove();
//or
$('.area').remove('.field');
if ($(this).val() == "Insurance") {
$(".area").append("<input class='field' name='cost' type='text' placeholder='Cost' />");
}
if ($(this).val() == "Damage <$100") {
$(".area").append("<input class='field' name='cost' type='text' placeholder='Cost' />");
}
});
and here is the html dropdown box:
<div id="area" class="area">
<strong>Cases:</strong><input name="cases" type=text placeholder="Cases ID" maxlength="7" style="width:129px;">
<br />
<strong>Claim:</strong>
<select class="claim" id="claim" name="claim">
<option value="">Select a Claim</option>
<option value="Insurance">Insurance</option>
<option value="Warrenty">Warrenty</option>
<option value="Damage <$100">Damage <$100</option>
</select>
I have attempted to edit the Javascript and a few other things but none seem to work. I am guessing its something that is simple but I am unsure about using Javascript at this moment so any help would be great.
Use
instead of
DEMO
Note
You’re comparing string so not need any special character.
Some more
Instead of
$(this).val()usethis.value, would be faster.