How to call the value of a javascript variable to use in src?

1

I have this function in js that takes the name of the video that the user chose

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
        vars[key] = value;
    });
    return vars;
}
var video = getUrlVars()["v"];
alert(video);

I would like to know how I can use the value obtained in the video variable in the src below:

<video width="502" height="360" id="Video1">
  <source src="COMO COLOCAR O VALOR DA VARIAVEL AQUI" type=video/webm>
</video>    
    
asked by anonymous 20.10.2016 / 03:12

2 answers

2

Set an id in <source id="valor" with this id, you will go in the javascript there in the function and append the following code:

document.getElementById("valor").setAttribute("src", nome da variavel);

After putting the name of the variable it will define within the attribute "src" the following value of the variable!

    
20.10.2016 / 04:09
2

Your function returns you the variable vars , is it a string? If I'm not wrong, I suppose you just set the variable as the value of the attribute ...

JQuerry

$("#Video1 > source").attr("src", vars);

Javascript

function myFunction() {
  var el = document.querySelector("#Video1 > source");
  el.setAttribute("src", vars);
}
    
20.10.2016 / 04:21