I have a select and get value from jquery and want to compare it with value in foreach if two value id is similar then active the selected value how can I do this is the foreach in blade:
I want two make a comparison in foreach loop to compare if the value of branch from jquery equal to value from foreach then selected it
<select name="branch">
@foreach($branchs as $branch)
<option value="{{$branch->id}}">{{$branch->title}}</option>
@endforeach
</select>
$.ajax({
url:getHref,
data:{id:id},
}).done(function(data) {
$.each(data, function(index,employee){
$( "input[name*='name']" ).val(employee.name );
$( "select[name='branch']").val(employee.branch_id);
$( "select[name='role_id']" ).val(employee.role_id);
});
});
});
You don't have to hard coded it since jquery will do the job for you
as you changed the value of select elemnt using jquery
then if the value of employee.branch_id meet the value of any option element
then it will automatically selected
$( "select[name='branch']").val(employee.branch_id);
and if you want to trigger change event
$( "select[name='branch']").val(employee.branch_id).change();