user1601716
user1601716

Reputation: 1993

auto fill input box with predetermined value

This is very close to Change Text Box Value Based on Select Input with Selected Attribute However I have an additional part that is not answered by that.

I have the code working so the dropdown fills in the input form with the dropdown's value. However I am not sure how to fill in the input form's value with something different.

So I am doing this

$(document).ready(function() {
    $("#subjob").live("change", function() {
        $("#account").val($(this).find("option:selected").attr("value"));
        });
            $('#subjob option[value=subjob1]').attr('selected','selected').change();

I want to make a list of what #account should say when #subjob is on an item, but I don't want it to say 'subjob1' in this case. I would want it to say 'please enter your account number'

Upvotes: 1

Views: 232

Answers (1)

Bryan Dellinger
Bryan Dellinger

Reputation: 5294

can you just add an html5 data attribute for what you want?

$(document).ready(function() {

    $("#name option").filter(function() {
        return $(this).val() == $("#firstname").val();
    }).attr('selected', true);

    $("#name").live("change", function() {

        $("#firstname").val($(this).find("option:selected").attr("data-inputvalue"));
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<select id="name" name="name"> 
<option value="" data-inputvalue="">Please select...</option> 
<option value="Elvis" data-inputvalue = "input Elvis">Elvis</option> 
<option value="Frank" data-inputvalue = "input Frank">Frank</option> 
<option value="Jim" data-inputvalue = "input Jim">Jim</option> 
</select>

<input type="text" id="firstname" name="firstname" value="input Elvis" readonly="readonly">

Upvotes: 2

Related Questions