Search code examples
jqueryhtmllaravellaravel-5.3

Append HTML data jQuery


This time I wrote html in controller when I check a checkbox I get data in console but I want to get it in the form.

I have checkbox in foreach loop

<input type='checkbox' value='{$student['id']}' name='student_ids[]' id='chkbx' required> {$student['name']}

when click on checkboxes data load accordingly

its my html

    $str .= '<div class="form-group">
    <label class="control-label col-md-3">
    Student
    </label>
    <div class="col-md-4">
        <select id="students-clicked" class="form-control select2-multiple" multiple name="students[]">
            <option value=""></option>';
            foreach ($students as $students) {
                $str .= "<option value='{$student->id}'>{$student->name}</option>";
            }

$str .=         '</select>
    </div>
</div>';

Jquery Method is as

    $('body').on('click', "#chkbx", function() {
    var class = $('input:checkbox:checked').map(function() {
        return this.value;
    }).get();
    $.ajax({
            url: '/trigger/studentsData',
            type: 'POST',
            data: {'trigger_type': 'click' , ids: class},
            success: function(result) {

            }
        });
});

Except jQuery everything is written in controller this time I am getting HTML data in console how can I append it in my html form it should also update according to checkbox selection


Solution

  • Try this :-

    <input type='checkbox' value='{$student['id']}' name='student_ids[]' id='chkbx' required/> {$student['name']}
    <div id="appendata">
     //your ajax data
    </div>
    

    Now in Success Function

    success: function(result) {
        $("#appendata").html(result); // this will replace when you select the checkbox
             or
        $("#appendata").append(result); // this will append when you select again from checkbox
    }
    

    Hope it helps