JQuery - how can I get the value of a src video?

I need to get the video value and change it to a new value ... for this 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 can not get the value. what is wrong with my code? In case I get a value, how can I change src?

any suggestion please?

+4
source share
4 answers

Try it,

Live demo

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

Based on OP comments, changing src file name

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 ); })​; 
+5
source

your video tag does not have a source attribute, so you never get it. Instead, get the src attribute of its internal source tags

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

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

+4
source
 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'); }); 
+1
source

Try the following:

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

Source: https://habr.com/ru/post/1440508/


All Articles