-3

Wanting to create a button. Once button click onevent of the value.

<button type="button" value="xxxx">Press</button>
<button type="button" value="yyyy">Press</button>

value of xxx from button will enter the textfield with the value of the button as per value.

<input type="textarea" name="textfield" id="text">
5
  • stackoverflow.com/questions/26227487/… Commented May 31, 2016 at 15:00
  • What have you tried? Where are you stuck? It sounds like you're just looking for JavaScript tutorials at this point, and there are many available. Commented May 31, 2016 at 15:00
  • i have tried creating a script but the event is not working to putting the value into the textbox @Alexis Commented May 31, 2016 at 15:01
  • Share your script then Commented May 31, 2016 at 15:04
  • is the same as the suggested answers but not sure why is the textarea not capturing my input. im not sharing the my actual model. else u will be accusing me for being lazy. am just asking to clarify the concept @Alexis Commented May 31, 2016 at 15:16

3 Answers 3

0

Define a function in Javascript

function fillText(value)
{
    document.getElementById('text').value = value;
}

And put that in the click event of button

<button type="button" value="xxxx" onclick="fillText(this.value)">Press</button>
<button type="button" value="yyyy" onclick="fillText(this.value)">Press</button>
Sign up to request clarification or add additional context in comments.

Comments

0

You need this

var btn1 = document.getElementById('btn1');
var btn2 = document.getElementById('btn2');

btn1.addEventListener('click', function(e) {
  document.getElementById('text').value = e.target.value;
});

btn2.addEventListener('click', function(e) {
  document.getElementById('text').value = e.target.value;
});
<button id="btn1" value="xxxx">Press</button>
<button id="btn2" value="yyyy">Press</button>

<input type="textarea" name="textfield" id="text">
Note : There is no need of type=button for <button> tag.

Comments

0

You have to use the document.GetElementById() method.

First, create a JavaScript function:

function transfer(element) {
  document.getElementById("text").value = element.value;
}

Now, make the button run that function when it is pressed with itself as a parameter.

<button type="button" value="xxxx" onclick="transfer(this)">Press</button>
<button type="button" value="yyyy" onclick="transfer(this)">Press</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.