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

Asked

Viewed 2,159 times

1

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

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 use the value obtained in the video variable in src below:

<video width="502" height="360" id="Video1">
  <source src="COMO COLOCAR O VALOR DA VARIAVEL AQUI" type=video/webm>
</video>    

2 answers

2


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

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

After placing the variable name it will set within the attribute "src" the following variable value!

  • well put the code at the end after Alert with the video variable so that is giving typeError, you know what can be wrong?

  • Can you tell me which line of code he’s quoting is wrong?

  • well I decided to just use window.load in the function

2

Your function returns 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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.