Q:

Dynamically injected form field does not get submitted while form submission

<%= simple_form_for @product, method: :post, url: save_item_path do |f| %>
    <tr>
        <td>
            Category
        </td>
        <td>
            <%= f.input :category_id, collection: Category.all, label: false, prompt: 'Select your category', required: true ,required: true %>
        </td>
    </tr>

    <div class="row " style="height:100px;width:100%">
        <div class="col-md-5 sub-cat-dropdown">
            <select class="select required sub-cat" required="required" name="product[sub_id]"><option value="">Select your sub category</option><option value="15">Bottle</option><option value="16">Charger</option></select>
        </div>
    </div>

    <div class="row">
        <div class="col-md-3 col-md-offset-5">
            <%= f.submit :submit %>
        </div>
    </div> 

<% end %>       

The form gets submitted with all the params that are required .. here is the params
{"utf8"=>"✓", "authenticity_token"=>"4gY7lWgSqo6j4UoZ78iv1nsaMq9TUXLXE2oQzlqqyh3I3oED3CxwM04cB1n5P+Nz81O3Vvk+/NDPxixNey/J9Q==", "product"=>{"name"=>"vamsi", "price"=>"456", "availability"=>"true", "about"=>"aerg", "ref"=>"456", "texture"=>"rdgg", "shipping"=>"ewataewrg", "category_id"=>"13", "notes"=>"feartg", "packaging"=>"wer5tyy", "sub_id"=>"15"}, "commit"=>"submit", "controller"=>"items", "action"=>"create"} 

In the above params hash, you can sub_id getting submitted. 
However, the sub category dropdown has to be generated dynamically, based on selection of the main category.
So the sub-cat-dropdown div is emptied like this
<div class="row " style="height:100px;width:100%">
        <div class="col-md-5 sub-cat-dropdown">

        </div>
</div>

I am trying to inject a select tag inside the sub-cat-dropdown, on click of the main category dropdown 
Here is the code I have written
$(document).ready(function(){

    //this function is supposed to create a dropdown of sub category
    //based on the main category dropdown

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

        var values = [];
        var inner_contents = [];

        if(this.value === undefined){
            return;
        }else{
            $main_category = this.value;
            $main_category_length = $cat_obj[$main_category].length;
            for (var i=0;i<$main_category_length;i++){
                values.push($cat_obj[$main_category][i].id);
                inner_contents.push($cat_obj[$main_category][i].name);
            }

            $('.sub-cat-dropdown').empty();
            inject_sub_category(values, inner_contents);
        }

    });

    // this function injects a dropdown of sub categories, just below the categories ... 

    function inject_sub_category(value_array, inner_html_array){

        var select = document.createElement("select");
        select.setAttribute("class","select required sub-cat");
        select.setAttribute("required","required");
        select.setAttribute("name","sub_id");

        // the above will create this 
        //  <select class ="select required form-control"
        //                  required = "required"       
        //                  name = "product[sub_id]"
        //                  id = "sub_cat_id"> 
        //  </select>               

        $('.sub-cat-dropdown').append(select);  

        // setting the default option 

        var default_option = document.createElement("option");
        default_option.setAttribute("value","")
        default_option.innerHTML = default_option.innerHTML + "Select your sub category";
        select.appendChild(default_option);
        // injecting <option value> Select your category </option> under select tag

        // creating options of subcategories, and injecting under the select element as children
        for(var i = 0 ; i< value_array.length ; i++){
            var option_element = document.createElement("option");
            option_element.setAttribute("value",value_array[i]);
            option_element.innerHTML = option_element.innerHTML + inner_html_array[i];
            select.appendChild(option_element)
        }

    }

        // this is supposed to read the json data of the categories and sub categories

        $.ajax({
        url: "http://localhost:3000/categories_api/show",
        type: 'GET',
        dataType: 'json', 
        success: function(res) {
                $cat_obj = res;
        }
    });

});

What above code does is it is supposed to insert a select dropdown .. inside the sub-cat-dropdown
The select tag that is inserted is looking like this, copied from browser
 <div class="col-md-5 sub-cat-dropdown" style="-webkit-user-select: text;"><select class="select required sub-cat" required="required" name="product[sub_id]"><option value="">Select your sub category</option><option value="15">Bottle</option><option value="16">Charger</option></select></div>

But, when the form submission happens, params does not have sub_id
{"utf8"=>"✓", "authenticity_token"=>"zUVF55KdvQ4l8nBjtlIcofqHECgd3/pIi7U97cF0xbrnnf9xJqNns8gPPSOgpVAEcs6V0bewdE9XGQFu4PHGUg==", "product"=>{"name"=>"jdkrfb", "price"=>"89", "availability"=>"true", "about"=>"sfukb", "ref"=>"78678", "texture"=>"jkdfbjkrbg", "shipping"=>"jkdfsk", "category_id"=>"12", "notes"=>"jhhdfvbj", "packaging"=>"sajkf"}, "commit"=>"submit", "controller"=>"items", "action"=>"create"}

I am having this issue for past several hours, any help will be greatly appreciated. 
My observations:
There is no problem with the ajax call, and the functions that are generating the select tag. I can see the DOM is having the generated select tag, and is a child of sub-cat-dropdown. 
The static select tag's sub_id is getting submitted, but for the dynamic one it is not the case. it's not submitted

A:

In javascript , you can't add event listener on dynamically created elements , even if you see them in DOM . That's why your dynamically created select option input will be in DOM but not be included in your form data . Try keeping the select input static and keep the options in it dynamic .
<div class="row " style="height:100px;width:100%">
 <div class="col-md-5 sub-cat-dropdown">
  <select class="select required form-control" required="required" name="product[sub_id]" id="sub_cat_id">
  </select>    
 </div>
</div>

instead of
<div class="row " style="height:100px;width:100%">
 <div class="col-md-5 sub-cat-dropdown">
 </div>
</div>

and then change the options inside select input through javascript -
function inject_sub_category(value_array, inner_html_array){

    // setting the default option 

    var default_option = document.createElement("option");
    default_option.setAttribute("value","")
    default_option.innerHTML = default_option.innerHTML + "Select your sub category";
    $("#sub_cat_id").append(default_option);
    // injecting <option value> Select your category </option> under select tag

    // creating options of subcategories, and injecting under the select element as children
    for(var i = 0 ; i< value_array.length ; i++){
        var option_element = document.createElement("option");
        option_element.setAttribute("value",value_array[i]);
        option_element.innerHTML = option_element.innerHTML + inner_html_array[i];
        $("#sub_cat_id").append(option_element)
    }

}

there is another way of doing it using event delegation . 

