views:

29

answers:

1

So, I want to use a jQuery function to collect a URL from a link's REL, and pass it to a element.

Collecting the REL and sending it to the is no problem... but what's required to trigger the element's load and play functions, from jQuery?

Here's what I have so far:

$(function(){   
    $('a.componentLink').click(function() {
        event.preventDefault();
        var vidURL = $(this).attr('rel');
        $('#myVideo > source').attr('src', vidURL);
        $('#myVideo').load();
        $('#myVideo').play();
    })
});

ATM, it doesn't play... I assume jQuery doesn't natively have access to the play function... but there must be a way around that.

+3  A: 

You need to call .play() on the element itself, not on the jQuery selection. Do the following:

$(function(){   
    $('a.componentLink').click(function() {
        event.preventDefault();
        var vidURL = $(this).attr('rel');
        $('#myVideo').attr('src', vidURL);
        var video = $('#myVideo').get(0);
        video.load();
        video.play();
    })
});
lonesomeday
Hmm... that doesn't seem to work either. I'm positive the variable is getting passed to the video element, but it's not being played...
Benjamin Allison
@Benjamin Can you link an example?
lonesomeday
@Benjamin Also, I am presuming this is an HTML5 video. Am I correct?
lonesomeday
Yes, html5 video! You can see a demo here by clicking on one of the two links in the playlist, below the video window (I decided to move to jQuery for this; before, I'd gotten this working with a "onclick" for each playlist item, but that's sloppy): http://roestudios.com/dump/tester.html
Benjamin Allison
Your markup is confusing -- you have Flash markup as a fallback for your video tag. When you set the source for the video, you need to set it in the `video` tag -- using the child `source` element will do absolutely nothing. I have edited my answer to change this. Note that you need to handle non-`video`-supporting browsers separately.
lonesomeday
Perfect! That works. I was setting the source of the source because that's what I had to do when using raw JS... but now it's working. Yeah, the flash fallback you see is because I'm using VideoJs to handle fallback; that's a whole other kettle of fish. Thanks for your help!
Benjamin Allison
Glad to have helped!
lonesomeday