1

Tengo que pasar esta variable {{$post->id}} que es un array y está dentro de un foreach. Con esto me devuelve todos los id de los post.

Expongo código:

$(document).ready(function() {  
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $('.like-post-btn').click(function(){
        var id = $(this).data('id');
        $.ajax({
            type:'POST',
            dataType: 'JSON',
            url:'/like-post',
            data:{id:id},
            success:function(data){
                if($.isEmptyObject(data.success)){
                    animation.playSegments([20, 26], true);
                    
                }else{
                    animation.playSegments([1, 15], true);
                }
            }
        });
    }); 
});

Al hacer un console.log(animation); y clico en el post 1, me devuelve ['animation1']. En el post 2 me devuelve ['animation2'],... así en cada uno que exista.

El problema está en animation.playSegments([1, 15], true); no tengo ni idea como añadir el id del {{$post->id}}. Manualmente funciona poniendo un 1 para el post 1: animation1.playSegments([1, 15], true); o un 2 para el post 2: animation2.playSegments([1, 15], true);

La idea sería algo así: animation{{$post->id}}.playSegments([1, 15], true); Pero eso no funciona. He probado esto: var animation = ['animation'+$(this).data('id')]; pero tampoco... aunque si hago un console.log(animation); me devuelve lo que quiero pero no me funciona.

Alguna ayudita? Gracias!! un saludo

2
  • pero porque dices que no funciona, te da algun mensaje de error?
    – David081
    el 2 dic. 2022 a las 11:31
  • @David081, si hago esto: var animation = ['animation'+$(this).data('id')]; Uncaught TypeError: animation.playSegments is not a function . Pero si hago esto animation1.playSegments([1, 15], true); funciona pero solo con uno post el 1
    – BOLIBICK
    el 2 dic. 2022 a las 11:36

1 respuesta 1

1

Creo que lo que buscas es esto:

this["animation" + id].playSegments([1, 15], true);

Es decir, intenta usar el this de modo global, pero si ves que da conflicto, entonces intenta estas otras opciones de globalThis, con window, self o frames:

window["animation" + id].playSegments([1, 15], true);
self["animation" + id].playSegments([1, 15], true);
frames["animation" + id].playSegments([1, 15], true);

La variable id la sacamos de antes, donde has puesto var id = $(this).data('id');.

Ejemplo:

let animation1 = document.getElementById("animation1")
let animation2 = document.getElementById("animation2")
let animation3 = document.getElementById("animation3")
let animation4 = document.getElementById("animation4")
let id1 = "1";
let id2 = "2";
let id3 = "3";
let id4 = "4";

this["animation" + id1].innerHTML = "Funciona " + id1
window["animation" + id2].innerHTML = "Funciona " + id2
self["animation" + id3].innerHTML = "Funciona " + id3
frames["animation" + id4].innerHTML = "Funciona " + id4
<div id="animation1"></div>
<div id="animation2"></div>
<div id="animation3"></div>
<div id="animation4"></div>

1
  • 1
    Me sirvió!! Muchísimas gracias por tu ayuda :)
    – BOLIBICK
    el 2 dic. 2022 a las 14:12

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.