Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - how i can get the video src value?

Tags:

jquery

video

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?

like image 270
3gwebtrain Avatar asked Oct 18 '12 10:10

3gwebtrain


3 Answers

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

like image 173
Amir Ismail Avatar answered Oct 30 '22 13:10

Amir Ismail


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 );    
})​;
like image 26
Adil Avatar answered Oct 30 '22 13:10

Adil


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');
});
like image 1
xdazz Avatar answered Oct 30 '22 13:10

xdazz