0

I'm trying to create a variable in my Javascript that uses the value assigned on data-start-time.

Here is my html:

        <li data-pile="1" id="kQKhpVWBjoQ" data-start-time="20" class="md-trigger md-setperspective">
        </li>

Here is my JS:

function playVideo(videoId, cb) {
    if(videoId) {
        myModal.find('.md-video').append($videoDiv);
        myModal.addClass('md-show');
        setTimeout(function () {
            console.log('#### id', videoId);
            var startTime = videoId.getAttribute('data-start-time');
            player.loadVideoById({'videoId': videoId, 'startSeconds': startTime});
            player.videoEnded = function () {
                cb && cb();
            };

            player.waitForChanges();
        }, 1000);
    }
}

If I create variable startTime and hardcode some value in my js the player works. However I can seem to figure out what is wrong with line:

var startTime = videoId.getAttribute('data-start-time');

All I need to do is get the value assigned in html inside data-start-time="..." for each "li" using the ids that vary according to the specific "li"

1
  • 1
    What is the videoId argument? If it's a string containing the id of the element then videoId.getAttribute() is an error. Try $("#"+videoId).attr('data-start-time'). Commented Oct 6, 2013 at 5:48

1 Answer 1

3

Try this:

var startTime = $('#'+videoId).prop('data-start-time');

Assuming what the function in receiving in videoId is a element ID then you need to get that element by the ID. Just naming the ID will not do it. You can also use plain javascript like this:

document.getElementById(videoId).getAttribute('data-start-time');
Sign up to request clarification or add additional context in comments.

1 Comment

@irm, glad I could help. Just updated my answer explaining more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.