0

I have two buttons and a text field. I want to update the display of the text field with the value of the buttons each time a button is pressed.

function displayTextfield() { 
  var amount;
  amount = document.getElementById('btn').value;
  document.getElementById('other-amount') = amount;
}
<button class="btn btn-default" id="btn" onclick='displayTextfield()' role="button" value="A">A</button>
<button class="btn btn-default" id="btn" onclick='displayTextfield()' role="button" value="B">B</button>
<input type="text" class="form-control donation-amount-input" placeholder="Other Amount" id="other-amount" onclick='displayTextfield()'/>

When I run the click the button I get the error:

Uncaught TypeError: Cannot read property 'value' of null

3
  • 1
    You do not have a #value anywhere in your HTML. Commented Oct 6, 2018 at 22:03
  • And you got 2 buttons with the same id ("btn"). Commented Oct 6, 2018 at 22:17
  • Change your third line inside the displayTextField function to document.getElementById('other-amount').value = amount; Commented Oct 6, 2018 at 22:39

2 Answers 2

1

just add id="value" to <input type="text" class="form...

Sign up to request clarification or add additional context in comments.

Comments

1

hit run snippet below you can use a selector (I used the class btn) to target the buttons you are interested in. then take advantage of $(this) to get the value of the clicked button

$('.btn').click(function() {
  $('#other-amount').val($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 <button class="btn btn-default" role="button" value="A">A</button>
    <button class="btn btn-default" role="button" value="B">B</button>
    <input type="text" class="form-control donation-amount-input" placeholder="Other Amount" id="other-amount" />

1 Comment

Thank you, this is a simple solution.

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.