Home > Software design >  Call a variable from laravel to js
Call a variable from laravel to js

Time:08-08

I'm trying to display an iframe inside a foreach and I need to pass the id of each item. If I put the id manually by putting an id number of the table it works perfectly, but my interest would be to pass a variable to it so I don't have to do it manually... I expose my code:

The number 16 would be the id manually and what I want is to pass the variable, this one: {{$comment->id}} but I don't know how to do it...

function postYourAdd () {
 var iframe = $("#forPostyouradd_"   16);
 iframe.attr("src", iframe.data("src")); 
}

When clicking the button, the iframe would be seen:

<button onclick="postYourAdd()" type="button">
show content
</button>
 
<iframe id="forPostyouradd_{{$comment->id}}" data-src="{{route('reactions.show', ['id' => $comment->id])}}" src="about:blank" width="500" height="200" style="background:#ffffff"></iframe>

CodePudding user response:

I hope that useful :

In foreach loop

<button onclick="postYourAdd(this)" type="button" data-id={{$comment->id}}>
show content
</button>
 
<iframe id="forPostyouradd_{{$comment->id}}" data-src="{{route('reactions.show', ['id' => $comment->id])}}" src="about:blank" width="500" height="200" style="background:#ffffff"></iframe>

In js file:

function postYourAdd (target) {
 let id = $(target).data('id');
 var iframe = $("#forPostyouradd_"   id);
 iframe.attr("src", iframe.data("src")); 
}
  • Related