Pain12
1 month ago
181
4
Tips

@forelse and javascript

Posted 1 month ago by Pain12

Hey guys, maybe you can help me understand how @forelse is or is not working for javascript.

My situation is something like this.

I have a forelse loop

@forelse($posts as $post)
           <a><img onclick="myFunction()" id="{{$post->id}}" src="/png/videoplayer.jpg" alt="video"
  		    data-toggle="modal" data-target="#modal-{{$post->id}}"></a>
			
	 	   opens modal with:
<h4>From {{$post->timefrom}} to {{$post->timeto}}</h4>
...

that gives me all my posts from my user through the controller.

Now I want to use a script on every post, that tests some stuff. My problem is that my script doesn't loop through all my posts even when I place the script in the forelse loop. My script only give the last post. So I think it just runs throw all posts and outputs the last one/ or first one depending on what is written in the controller.

I could create a separate loop for the script but how would I connect the script with the output above? The script could be something like that.

function myFunction() {
         var test = "{{$post->timefrom}}"; 
         var test2 = "{{ $post->timeto }}"; 
         
         alert("start datum = " + test  + " end datum = " + test2 );

Now when I click on the link I want my script to run and give me an alert with my variables that should exactly be the same as above.

Hope you understand what I mean =D Basically match my data in the html and my script

Please sign in or create an account to participate in this conversation.