3gwebtrain
3gwebtrain

Reputation: 15303

Jquery - how i can get the video src value?

i need to get the value of a video and need to change in to new value... for that i used this function:

var video = $('#task2ResultVideo').get(0);

console.log($(video).attr('poster')); // i am getting this

    $(video).attr('src',function(num,val){
        console.log(num,val)        // i am not getting this..
    }) ​

HTML:

<video id="task2ResultVideo" autobuffer poster="img/task2-results-host-poster.jpg">
    <source src="Video/webm/Task_2.4a_Host_treated.webm" type="video/webm" />
    <source src="Video/ogv/Task_2.4a_Host_treated.theora.ogv" type="video/ogg" />
    <source src="Video/MP4/Task_2.4a_Host_treated.mp4" type="video/mp4" />
</video>​

But i am not able to get the values. what is wrong with my code? in case if i get the value how can i change the src?

any suggestion please?

Upvotes: 8

Views: 26615

Answers (4)

Adil
Adil

Reputation: 148140

Try this,

Live Demo

$('video source').each(function(num,val){
    console.log($(this).attr('src'));        // i am not getting this..
    $(this).attr('src', 'newSourceValue')
});

Based on the comments by OP, changing the src filename

Live Demo

$('video source').each(function(num,val){
    console.log($(this).attr('src'));        // i am not getting this..
    strSrc = $(this).attr('src'); 
    strPath = strSrc.substring(0, strSrc.lastIndexOf('/'));
    strExtenion = strSrc.substring(strSrc.lastIndexOf('.'));
    $(this).attr('src', strPath + "/" + "newFileName" + strExtenion );    
})​;

Upvotes: 6

Amir Ismail
Amir Ismail

Reputation: 3883

your video tag has not any source attribute so you will never get it. Instead get src attribute of its inner source tags

$('video').find('Source:first').attr('src');

this will get the value of src attribute of first source tag inside video tag

Upvotes: 10

xdazz
xdazz

Reputation: 160863

var video = $('#task2ResultVideo');

video.find('source').each(function() {
  console.log($(this).attr('src'));
});​

// if you want to get array of the src
var arr = video.find('source').map(function() {
    return $(this).attr('src');
});

Upvotes: 1

akluth
akluth

Reputation: 8583

Try this:

$(video).children().each(function(index) {
    console.log(index + " " + $(this).attr('src'));
});

Upvotes: 0

Related Questions