Reputation: 350
I am trying to make js dynamic inside a laravel loop.
My intention is that when selecting a product from the "product" select, load its value.
Laravel view:
@for ($i = 0; $i <= sizeof($fibra["voz"]) - 1; $i++)
<div class="form-group">
<label for="wifi">Wifi {{ $i + 1 }}</label>
<select class="form-control wifi" name="wifi" data-repeat="{{ $i }}" required>
<option selected hidden disabled>Elige un wifi...</option>
@foreach ($wifis_inversor as $wifi_inversor)
<option data-comission="{{ $wifi_inversor->commission }}" value="{{ $wifi_inversor->name }}">{{ $wifi_inversor->name }}</option>
@endforeach
</select>
</div>
@endfor
My jquery code:
var comision = $('.comision_total');
var comision_total = 0;
var comision_total_wifi = 0;
$('.wifi').change(function () {
var idRepeat = $('.wifi').attr("data-repeat");
var comision_wifi = $('.wifi[data-repeat=' + idRepeat + ']').find(':selected').data('comission');
comision_anterior = comision_total_wifi;
comision_total_wifi = comision_wifi;
comision_total = comision_total + comision_total_wifi - comision_anterior;
comision.val(comision_total);
});
My intention is to add the "commission" dynamically whenever, in any of the selects, the product changes. But it only works for me for the first select. The second does not. How can I solve it?
Upvotes: 0
Views: 340
Reputation: 219026
This will find the attribute value from the first matching element, not the one which happens to be what was changed:
$('.wifi').attr("data-repeat")
To get the data-repeat
value from the changed element, refer to this
:
$(this).attr("data-repeat")
Or:
$(this).data("repeat")
Upvotes: 4