1

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 &lt;$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 &lt;$100">Damage &lt;$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.

1 Answer 1

2

Use

$(this).val() == "Damage <$100"

instead of

$(this).val() == "Damage &lt;$100"

DEMO

Note

You're comparing string so not need any special character.

Some more

Instead of $(this).val() use this.value, would be faster.

Sign up to request clarification or add additional context in comments.

7 Comments

i have attempeted to use the code u used in the demo and i noticed once i placed it with the rest of my jquery code the Inserance code would would work not the one for damge <100... any idea why this might be
@Matthew that means above code is not working? is it possible to post your whole jQuery code in fiddle?
what do u mean make a find replace operation? and yes tis quite a fair bit of code
jsfiddle.net/matth4ck3r/p22Y7 here is the link but for some reason it seems to work once its placed in jsfiddle but on my machine it will not work
@Matthew It seems your code is just working fine. So where is the issue?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.