uma uma - 3 months ago 10
HTML Question

Is there any way to pass PHP object as JavaScript parameter in HTML?

I do project using Laravel 5. I show table from view and I want to when click this link pass it is data value all data to JavaScript function. I am try to several way but cannot do it.

@foreach ($basl_officers as $basl_officer)
<tr>
<td><a href="#" onClick="functionOne($basl_officer)" >{{ $basl_officer->officerName }} </a></td>
<td align='center'>
{!! Form::open(['method' => 'DELETE', 'route'=>['basl_officers_page.destroy',$basl_officer->id]]) !!}
<a href="{{route('basl_officers_page.edit',$basl_officer->id)}}" class="btn btn-default btn-sm"> <span class="glyphicon glyphicon-pencil"></span> </a> &nbsp &nbsp
<button type="submit" class="btn btn-default btn-sm" onclick="return confirm('Are you sure?')"> <span class="glyphicon glyphicon-trash"></span> </button>
{!! Form::close() !!}
</td>
</tr>
@endforeach


JavaScript Code

<script type="text/javascript">
function functionOne(x) {
alert('You clicked the top text' + x);
}
function functionTwo() {
alert('You clicked the bottom text');
}



Answer

You could try this:

onClick="functionOne({{ json_encode($basl_officer) }})"