I want to show the detailed address of a selected recipient name from a dropdown menu. So, if I choose address A, it will display the details of address A. I'm already working on the code but the issue is it doesn't show the correct value, instead it shows the last record in the database. Here's the code:
Blade
<select class="address-detail form-control" id="address" name="address_detail" data-target=".detail-info-address">
<option value="option_select">Select Address</option>
@foreach($addresses as $address)
<option value="{{$address->id}}" data-show=".info-address">{{$address->recipient_name}}
</option>
@endforeach
</select>
<div class="detail-info-address">
<div class="info-address hide text-left">
<p>Recipient Name: {{$address->recipient_name}}</p>
<p>Contact Number: {{$address->contact_number}}</p>
<p>Address: {{$address->address}}</p>
<p>Address Note (optional): {{$address->address_note}}</p>
<p>Post Code: {{$address->post_code}}</p>
<p>Province: {{$address->province}}</p>
<p>City: {{$address->city}}</p>
<p>District: {{$address->district}}</p>
<br>
</div>
</div>
Controller
public function buynow($id) {
$addresses = Address_Delivery_Users::where('user_id', '=', Auth::user()->id)->get();
return view('/transactions/delivery', compact('addresses'));
}
Javascript
<script>
$(document).on('change', '.address-detail', function() {
var target = $(this).data('target');
var show = $("option:selected", this).data('show');
$(target).children().addClass('hide');
$(show).removeClass('hide');
});
$(document).ready(function(){
$('.address-detail').trigger('change');
});
</script>