Capture id from an img tag list with jquery?

0

The application what it tries to do is against a series of stories of some citizens in a video clip series, now, what I'm trying to do is show thumbnails.

I'm trying to capture the value src of an img tag, the thing is that the id of the img I have it in a database, trying to pass that id by a function so with jquery try to capture the src He does not do it, he tells me that it's undefined. can someone tell me how I can do to capture the id dynamically.

function marcadoRegionImagen(nombre_video) {
    nombre_video = nombre_video.toLowerCase();
    var src = $(nombre_video).attr('src');
    console.log('funciona? ' + src);
}

This function I call when I start the player.

rep_one.onplay = function () {
            if (parseInt(estado_id_video) >= long.length) {
                getVideo();
            }
            rep = 1;
            $('#video_two').attr('src', video.video[parseInt(estado_id_video) + 1].R);
            estado_id_video = parseInt(estado_id_video) + 1;
            //Aqui le paso el id que vide desde la base de datos
            marcadoRegionImagen(video.video[parseInt(estado_id_video)].V);
        }

This way I would have to allow myself to show the user which of the different videos he is currently in.

On the side of the front-end you have a list of images, more or less like that.

But I'm passing him the id but nothing, he always returns undefined, Can someone tell me how I can do?

    
asked by Jhonny Luis 27.06.2018 в 02:06
source

2 answers

0

If you are passing the ID, you must concatenate it with the hashtag # like this:

function marcadoRegionImagen(nombre_video) {
    nombre_video = nombre_video.toLowerCase();
    var src = $("#" + nombre_video).attr('src');
    console.log('funciona? ' + src);
}
marcadoRegionImagen("test");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img id="test" src="hello"></img>
    
answered by 27.06.2018 в 02:10
0

Apparently you have problems with the characters sent to the selector in JQuery, remember that a point refers to a class, so this happens because you are sending the id with a point included,

$('#video_01.mp4');  // incluirá una clase mp4 a la búsqueda

To solve, you can apply a regular expression to replace the point with the \ and escape that character.

nombre_video.toLowerCase().replace( /(\.)/g, "\$1" );

function marcadoRegionImagen(nombre_video) {
    nombre_video = nombre_video.toLowerCase().replace( /(\.)/g, "\$1" );
    var src = $("#" + nombre_video).attr('src');
    console.log('funciona? ' + src);
}
marcadoRegionImagen("video_01.mp4");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="video_01.mp4" src="hello"></img>

Although it may be a solution, I would recommend avoiding id with a point included.

    
answered by 27.06.2018 в 02:38