How do i use a javascript variable within blade curly braces

I have a video player site and i have a "next" button which should stop the video , change the src directing to the next video and play it using javascript but I cant get around using a javascript loop to change the src.

Heres my blade file

<section class="player">
    <video id="myvideo" class="playing-video" src="{{url('songs/'.$songs[0]->filename)}}" controls autoplay onended="nextVideo()">
    <button id="next" style="background-color: transparent;border: none;color: white;" onclick="nextVideo()" type="button">
    <h2><i class="fas fa-chevron-right"></i>Next</h2></button>

and heres the javascript which is not working

var i = 0;
var myVideo = document.getElementById("myvideo");

function nextVideo() {
  myVideo.setAttribute("src", "http://localhost/Laravel/anime/public/songs/" + {{ $songs[i] -> filename }});

I know I cant use a javascript variable inside the blade curly braces directly but I am out of ideas!



Store the Laravel collection as a data point on the button:

<button id="next" data-songs="{{json_encode($songs)}}" ... etc />

Then pull the JS object out within the nextVideo() JS method and loop on it (all JS):

function nextVideo() {
    var songs = JSON.parse( //get the data from the #next button )
    //loop using the JS songs object instead of the blade {{}}

Try using Toggle Button

  <button onclick="Toggle()">Next</button>


  var i =0;

  function toggle(){
    i=++i;  // each time you press button the variable i increments value by one (inside brackets only)

        videoStop(); // Function to stop video
    }else if(i===2){
        videoNext(); // Create a function which will contain url of next video , autoplay functions
            i=0; // return i value to 0 and continue using the toggle button


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.