0

I have dynamically created inputs/selects and these form fields contain brackets in their names as they are arrays. I want to refer to these selects so that I can initiate them as select2 objects but cant even get the selector right. As you can see I need the selector to be dynamic to include the initial array element "[0]", "[1]", etc. but to include the [course] end so I can specify the code for all the course select2 elements (will do the same for the [module] select2 elements, but that code will be different - so its not as simple as apply the same select2 code for all selects).

Hence my attempt to use a selector of "all select elements within the #modal-course-grades-list-body that begin with 'new_grades' and ends in '[course]' - but the square brackets is causing issues:

$("#modal-course-grades-list-body select[id^=new_grades][id$=[course]]").select2()

Error: Uncaught Error: Syntax error, unrecognized expression: #modal-course-grades-list-body select[id^=new_grades][id$=[course]]

(Dynamically Created) HTML:

<tbody id="modal-course-grades-list-body">
<tr>
    <td>Bachelor of Commerce</td>
    <td>Accounting 1A</td>
    <td>2019</td>
    <td id="course-row-1"><input class="form-control kt-inputmask decimal" id="module_grades[3663]" name="module_grades[3663]"></td>
</tr>
<tr>
    <td>Bachelor of Commerce</td>
    <td>Accounting 1B</td>
    <td>2019</td>
    <td id="course-row-2"><input class="form-control kt-inputmask decimal" id="module_grades[3664]" name="module_grades[3664]"></td>
</tr>
<tr>
    <td><select class="form-control" id="new_grades[0][course]" name="new_grades[0][course]" placeholder="Course"></select></td>
    <td><select class="form-control" id="new_grades[0][module]" name="new_grades[0][module]" placeholder="Module"></select></td>
    <td><input class="form-control" id="new_grades[0][year]" name="new_grades[0][year]" placeholder="Year"></td>
    <td><input class="form-control kt-inputmask decimal" id="new_grades[0][grade]" name="new_grades[0][grade]"></td>
</tr>
<tr>
    <td><select class="form-control" id="new_grades[1][course]" name="new_grades[1][course]" placeholder="Course"></select></td>
    <td><select class="form-control" id="new_grades[1][module]" name="new_grades[1][module]" placeholder="Module"></select></td>
    <td><input class="form-control" id="new_grades[1][year]" name="new_grades[1][year]" placeholder="Year"></td>
    <td><input class="form-control kt-inputmask decimal" id="new_grades[1][grade]" name="new_grades[1][grade]"></td>
</tr>
</tbody>        

jQuery:

    $("#modal-course-grades-list-body select[id^=new_grades][id$=[course]]").select2({
        placeholder: "Select A Course",
        ajax: {
            url: "{{route('baadmin.students.get_student_bursary_enrolment_courses')}}",
            type: "post",
            dataType: 'json',
            delay: 250,
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            data: function (params) {
                return {
                    parent_id: $("#student_bursary_enrolment_id").val(),
                    search: params.term // search term
                };
            },
            processResults: function (response) {
                return {
                    results: response
                };
            },
            cache: true,
            tags: true,
            createTag: function (params) {
                return {
                    id: params.term,
                    text: params.term,
                    newOption: true
                }
            },
            templateResult: function (data) {
                var $result = $("<span></span>");

                $result.text(data.text);

                if (data.newOption) {
                    $result.append(" <em>(new)</em>");
                }

                return $result;
            }
        }
    });
SupaMonkey
  • 876
  • 2
  • 9
  • 25
  • 1
    Can you not just avoid the problem entirely and simplify the code just by using common class attributes on all the relevant `select` instead? – Rory McCrossan Sep 03 '21 at 09:18
  • if I do that @CarstenLøvboAndersen - then it doesnt trigger / match. – SupaMonkey Sep 03 '21 at 09:27
  • 1
    `[id$='\[course\]']` https://jsfiddle.net/2fhcn06u/ but save yourself a ton of headaches and just add classes, ` – freedomn-m Sep 03 '21 at 09:35
  • 1
    Does this answer your question? [jQuery selector for inputs with square brackets in the name attribute](https://stackoverflow.com/questions/2364982/jquery-selector-for-inputs-with-square-brackets-in-the-name-attribute) – freedomn-m Sep 03 '21 at 09:40
  • That did the trick @freedomn-m - thanks! – SupaMonkey Sep 03 '21 at 09:52

0 Answers0