I need some help about this code
I have some code in blade view
@foreach($cart as $ct)
<button type="button" id="edit{{$ct->id}}" class="btn-btn-delete">
<i class="mr-1 fas fa-edit"></i>Edit
</button>
//when I {{$ct->id}} at this point, it return id of current product
<div class="form-popup" id="myForm{{$ct->id}}">
//however, when I {{$ct->id}} at this point, it return id of the last product
<form action="{{route('cart.update',$ct->id)}}" class="form-container" method="post">
@csrf
@method('patch')
<h1>Edit information</h1>
<input type="text" name="name" value="{{$ct->name}}">
<button type="submit" class="btn">change</button>
<button type="button" class="btn cancel" id="close{{$ct->id}}">Close</button>
</form>
</div>
<script>
var ID = {{$ct->id}};
var code = 'edit'+ID;
var end = 'close'+ID;
var form = 'myForm'+ID;
document.getElementById(code).addEventListener("click",function(){
document.getElementById(form).style.display = "block";
});
document.getElementById(end).addEventListener("click",function(){
document.getElementById(form).style.display = "none";
});
</script>
@endforeach
when I run my code and click on Edit
button the expected value in input
field of each row must be different. However, it all gets the value of the last column in the database.
How I can fix it?
addEventListener
instead of inlineonclick
would also be helpfulID
variable string.var ID = "{{$ct->id}}";
so it is a valid string value in javascript