How to assign jQuery value to a textarea

Asked

Viewed 103 times

1

I have a plan form, and I would like that by clicking acquire, it already fill out a brief message on the form. I’m trying this way, but it doesn’t return values

window.onload = function (){

var mensagem = document.getElementById('mensagem');
var button = document.getElementById('button');
var assinatura = '\n\nParabéns!!\nEscolheu o plano basic..'

button.addEventListener('click', function() {
  mensagem.value += assinatura;

});
}
<form>
      <textarea id="name" name="testa"></textarea>
      <button id="teste" type="button">Testando</button>
</form>

(Remembering that the above is just a test)

1 answer

1

Yes, but, you didn’t put the names correctly Document.getElementById takes the element by the id, example:

Html:

<button id="btn" />

Javascript:

var btn = document.getElementById("btn"); // btn é o id do button

Now with the modifications to your code:

window.onload = function() {
  var mensagem = document.getElementById('name');
  var button = document.getElementById('teste');
  var assinatura = 'Parabéns!!\nEscolheu o plano basic..'
  button.addEventListener('click', function() {
    mensagem.value = assinatura;
  });
}
<form>
      <textarea id="name" name="testa"></textarea>
      <button id="teste" type="button">Testando</button>
</form>

Observing: in your code you put id="name" and made the search document.getElementById('mensagem') and the button was placed on id="teste" and made the search document.getElementById('button'), I mean, you missed the name by which you should reference your code

Reference:

Browser other questions tagged

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