Currently, I am working with rails4 and have encountered an issue related to a dropdown list. My goal is to automatically select the radio_button of the first child element when a parent item is chosen from the dropdown menu. Essentially, the dropdown consists of parent items and upon selection of each parent, a corresponding list of children is displayed. Each child element is looped through and displayed individually. Using JavaScript, I am able to filter and display only those child elements that belong to the selected parent.
In the template code, whenever the parent_id is changed, the function " $("select[id='parent_<%[email protected]%>']").change" is triggered and the child elements in the template are as follows:
<div class="child<%= @parent.id %>" style="display:none">
<%=radio_button :child_name, @parent.id, child.name ,id: "child"%><%=child.name%>
</div>
$("select[id='parent_<%<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e433e0e1f0c1b100a50171a">[email protected]</a>%>']").change(function(){
$(".child"+ this.value).each(function(){
$(this).show();
$(this).firstElementChild.setAttribute("checked","checked"); //This part seems to be malfunctioning
});
});