Oliver Levay Oliver Levay - 5 months ago 10
HTML Question

Add a user variable to a link in HTML

I'm trying to make a script where a user types in a string that will be added to a link. I don't know much about Javascript or HTML so this example will be really weird.

var base = "https://www.youtube.com/watch?v=";

var userInput = ReadForm();

button {


<link>base + userInput</link>;

}


So lets say the user enters "dQw4w9WgXcQ" the link would be:

var base = "https://www.youtube.com/watch?v="

var user input = "dQw4w9WgXcQ"


Link= https://www.youtube.com/watch?v=dQw4w9WgXcQ

Any help is appreciated :)

Answer

You can do it like this

$("#add").click(function() {
  var link;
  var utubeLink = "https://www.youtube.com/watch?v=";
  link = utubeLink + $("#get").val();
  $("#display").text(link);
  console.log(link);
});

$("#go").click(function() {
  var utubeLink = "https://www.youtube.com/watch?v=";
  link = utubeLink + $("#get").val();
  window.open(link);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="get">
<button id="add">
  Add
</button>
<button id="go">
  Go
</button>
<br>
<br>
<text id="display"></text>

Here is the jsFiddle

Hope it helps :)