Hyosoka Poipo Hyosoka Poipo - 3 months ago 19
Javascript Question

How to get selected checkbox ID from some checkboxes in Laravel 5.2 using JQuery

I have made a view in laravel 5.2 like this :

<table class="table table-condensed table-bordered table-hover">
<tr>
<th>Report</th>
<th>Type</th>
<th>Date</th>
</tr>
<?php $index = 0; ?>
@foreach($tableContent as $data)
<?php $index++; ?>
<tr>
<td><input type="checkbox" name="reports-[{{$data->id}}]" id="{{$data->id}}" >{{$data->title}}</input></td>
<td><b>{{$data->report_type}}</b></td>
<td>{{$data->created_at}}</td>
</tr>
@endforeach

</table>


And then I want to get the id of checked checkbox in my controller. I have tried to use these code :

console.log($("input[name=reports-1]").attr("id"));
console.log($("input[name=reports-1]").val());


But those are undefined... Is there a way to get the ID of these checkboxes..?? Thanks in advance :)

I also have tried this :

console.log($("input[name=reports-1]").map(function()
{
return this.id;
}));


But it returns an object, not the checkbox ID

Answer

Take a look at your code. name="reports-[{{$data->id}}]" should be replace as name="reports-{{$data->id}}". In your name value in the checkbox input tag, it includes []. But, there is no [] in your console.log. Of course, it will return undefined as it will not match with any of your name value in the checkbox input tag.

Comments