Pick up value or button name with Jquery

Asked

Viewed 10,476 times

1

Does anyone know how I could catch the value of a button using Jquery?

What I want to do is use this value in a textarea, so it doesn’t necessarily have to be the value, the name would also serve.

I want you to Function like this: I have a list with several Buttons, and next to a textarea. What I want is when clicked on the button (each with its own value or name) appear the value/name in textarea next door. And what I’m not getting is to take the value/name of button by clicking on the button.

For that I already have each boot with same id, and in Jquery use the function .click()

Could someone help me?

Grateful from now on!

2 answers

3


You can get the properties of the button directly on click. Some properties are possible with attr or prop. And then assign to the textarea.

Example

$('button').click(function(){
  var valor = $(this).val();
  var nome = $(this).attr('name');
  $('#area').val(valor + ' ' + nome)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button value="1" name="um">Um</button>
<button value="2" name="dois">Dois</button>
<button value="3" name="tres">Tres</button>
<textarea id="area"></textarea>

Obs: Unable to use the even id in various elements.

  • 1

    It worked here @Lucas Costa. I was trying to get the boot with the id. Valeu!

0

There’s this shape that’s very simple.

<!DOCTYPE html>
<html>
<body>

<p>CLIQUE NO BOTAO E RETORNA O VALOR EM UMA DIV</p>

<button id="meuBotao" value="Teste do Botão" onclick="retornaValor()">Teste</button>

<div id="demonstracao"></div>

<script>
function retornaValor() {
  var x = document.getElementById("meuBotao").value;
  document.getElementById("demonstracao").innerHTML = x;
}
</script>

</body>
</html>

Browser other questions tagged

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