How can I use a javascript variable 'x' inside an @if of Laravel? I tried to do it inside an @if and outside. Outside, it works perfectly, but I really need to do an action based on this condition.
var x = document.getElementById("IdPrize").value;
var mySpan = document.createElement("span");
mySpan.innerHTML = "\
@foreach (\App\Prize::all() as $prize)\
@if ($prize->id == "+x+")\
<p>{{$prize->name}} is the choosen one!</p>\
@endif\
@endforeach";
Is there an specific reason for me not getting the variable 'x' value inside @if and @foreach? What can be done for it to work?
If I do it like the code below, it works perfectly.
var x = document.getElementById("IdPrize").value;
var mySpan = document.createElement("span");
mySpan.innerHTML = "\
@foreach (\App\Prize::all() as $prize)\
<p>"+x+"</p>\
@endforeach";
Thank you!