$(document).ready(function () { var selected; var selectedCatId = $('select[name=filter_category] option').filter(':selected').val(); if (selectedCatId != "" && selectedCatId != undefined) { $.ajax({ type: 'POST', url: 'get-sub-cats/' + selectedCatId, data: 'id=' + selectedCatId, success: function (data) { if (data != "") { $('select[name=filter_sub_category]').html(''); $('select[name=filter_sub_category]').append(data); } else { $('select[name=filter_sub_category]').html(''); } } }); } if (typeof search_category_url !== "undefined") { $("select[name='parent_category[]']").select2({ ajax: { url: search_category_url, type: "GET", data: function (params) { if ($(this).val() != null) selected = $(this).val().join("-") return { q: params.term, // search term selected: selected }; }, processResults: function (data) { return { results: $.map(data.category, function (item) { return { text: item.parents, id: item.id } }) }; }, cache: true }, allowClear: true, theme: "classic", placeholder: "All", minimumInputLength: 3 }); } if (window.location.pathname != "/admin/customfields/create" && typeof selected_category !== "undefined") { jsonArray = JSON.parse(selected_category.replace(/"/g, '"')); $('#parentCat').html(convertObjectToSelectOptions(jsonArray)).trigger('change'); } groupOptions() }); function groupOptions() { if (typeof chooseAnOptionTrans !== "undefined") { $('#type').html(` `) } } function convertObjectToSelectOptions(values) { var htmlTags = ''; $.each(values, function (index, obj) { for (var tag in obj) { htmlTags += ''; } }); return htmlTags; } $(document.body).on('click', 'select[name=filter_category]', function () { var selectedCatId = $('select[name=filter_category] option').filter(':selected').val(); if (selectedCatId != "") { $.ajax({ type: 'POST', url: 'get-sub-cats/' + selectedCatId, data: 'id=' + selectedCatId, success: function (data) { if (data != "") { $('select[name=filter_sub_category]').html(''); $('select[name=filter_sub_category]').append(data); } else { $('select[name=filter_sub_category]').html(''); } } }); } });