ImPuLsE
ImPuLsE

Reputation: 173

How dynamicly add input field from select option change

I have multiple select statements. When I select an option, I need to then dynamically add input fields below, with option text from the select. Thanks for help. This is what I have:

$('#tep_select').on('change',function(){

        if($('#tep_select').val() == 1){

                $('#tep_select_options').append('<input id="tep_select_options_'+1+'" type="text">');
            }else{
                $('#tep_select_options_'+1+'').remove();


 }});

How can the input control dynamically get its text label from the select's options?

Upvotes: 0

Views: 99

Answers (1)

bbailes
bbailes

Reputation: 371

Try this:

$("#tep_select").on("change", function(){

    if($(this).val() === "1") {
        $("<input>")
            .attr("id", "tep_select_options_1")
            .attr("type", "text")
            .val($(this).text())
            .appendTo("#tep_select_options");
    }
    else {
        $("#tep_select_options_"+$(this).val()).remove();
    }

});

Upvotes: 1

Related Questions