OK, I have an issue with some code and I'm really not sure how to code the outcome that I want to achieve.
I'm working on a site using Laravel 5.2 and I'm having an issue with hiding and showing specific divs based on the status of a checkbox. These are in a @foreach loop but I'm fairly sure that the laravel/@foreach isn't the issue, it's my lack of jQuery experience.
So here's some code.
@foreach ($event->extras()->get() as $extra)
<p>{{ $extra->name }}</p>
<p>{{ $extra->cost }}</p>
<p><input type="checkbox" name="extra-checkbox" id="extra-checkbox" value=""></p>
<div id="extra-info">
@if ($extra->infoRequired == "1")
<input type="text" name="extra-info-text" class="form-control">
@endif
</div>
@endforeach
The issue I'm having is that the $extra variable ID's ($extra->id) in the loop could be very different so a for-loop in the javascript is out. Each checkbox needs to show/hide only the specific "extra-info" div it is associated with.
How do I modify the following code to make it work for each individual $extra independently? I've considered putting the $extra->id into the class or div id but I can't figure out how to then implement it in the javascript.
$(document).ready(function() {
$('#extra-checkbox').change(function() {
if(this.checked)
$('#extra-info').fadeIn('fast');
else
$('#extra-info').fadeOut('fast');
Edit
I've added an id to the html as it's omission was my copying error. The issue I need to get my head around is that there will be multiple checkboxes (they're actually in a table, not <p>, but I was trying to make the code readable). Each checkbox is associated with an $extra and each $extra has it's own div that needs to be shown when the checkbox is checked.
divelement and makeextra-infoa class instead of an id. Then the checkbox handler can use this jquery expression to get the specific element:$(this).parent().children().filter('.extra-info')