Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

79
Visualizações
Javascript Link to Href maker not working

I'm trying to make a simple link to html maker, but the thing is variables aren't working

var text1 = document.getElementById("text1");
var link1 = document.getElementById("link1");
var input = document.getElementById("thetext");
var flink1 = "<a href='" + link1.value + "'>" + text1.value + "</a>";

function generate() {
  input.value = flink1
}
input,
textarea {
  width: 100%;
  height: 50px
}
<textarea id="thetext" class=""></textarea>
<br/><br/>
<input id="link1" placeholder="link"></input><br/><br/>
<input id="text1" placeholder="link title"></input><br/><br/>
<button id="" onclick="generate()"></button>

Can anyone say what's the issue

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This is your answer.

function generate() {
var text1 = document.getElementById("text1");
var link1 = document.getElementById("link1");
var input = document.getElementById("thetext");
var flink1 = "<a href='" + link1.value + "'>" + text1.value + "</a>";
input.value = flink1;
}
input,
textarea {
  width: 100%;
  height: 50px
}
<textarea id="thetext" class=""></textarea>
<br/><br/>
<input id="link1" placeholder="link"></input><br/><br/>
<input id="text1" placeholder="link title"></input><br/><br/>
<button id="" onclick="generate()">Generate</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

Your flink1 will always contain empty strings for href and innerText because your generate doesn't update its value upon user input.

Move the variable definition inside generate call, and it'll work as intended.

var text1 = document.getElementById("text1");
var link1 = document.getElementById("link1");
var input = document.getElementById("thetext");

function generate() {
  var flink1 = "<a href='" + link1.value + "'>" + text1.value + "</a>";
  input.value = flink1
}
input,
textarea {
  width: 100%;
  height: 50px
}
<textarea id="thetext" class=""></textarea>
<br/><br/>
<input id="link1" placeholder="link"></input><br/><br/>
<input id="text1" placeholder="link title"></input><br/><br/>
<button id="" onclick="generate()">Generate</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda