skip to Main Content

I wanna add elements to the array in laravel blade template. here is my script:

<script>
  selected_arr = [];
</script>

I wanna get an array like [1,2,3], I tried to use array_push, the code is here:

<script>
  function select_array (id){
    selected_arr=array_push(selected_arr, id);
  }
</script>

the result of selected_arr is still null. Can you please help me to figure it out? Many Thanks!

2

Answers


  1. Your question is not clear, because laravel blade is ssr.

    Client-side by using js:

    var selectedArray = [];
    
    //option1
    selectedArray = [...selectedArray, 1, 2];
    
    //option2
    selectedArray.push(3)
    Login or Signup to reply.
  2. If you need to share data with the server you need a controller behind it, then you need ajax calls to sync view variables with the contoller.

    If you only need the array variable on client side you can use javascript function like

       selected_arr.push(id)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search