1

I am trying to make an input modify each time I click on a button. Kind of like a calculator. The will start at 0. When I click on the button "7" it will change its value to 7, when I click on "4" it will change to 74. Basically like a calculator.

I made this code that does modify the value of the input, however I can't seem to find how I can append more values to that values. Here is my code. Could someone help me?

<input class="normal-input" type="number" step="0.01"> <!-- Value is NULL-->
<button value="7" class="my-buttons" type="button"> 7 </button> <!--Button to change the value of the input-->

 $('.my-buttons').click(function(){
     $(".normal-input").attr("value", $(this).attr('value'));
 }); <!-- The actual function. -->

As you can see the function completely replaces the previous value for the new one. I want it to append the values like in a calculator.

3 Answers 3

1

I would suggest you to try doing this:

  • Initialize value to zero
  • whenever click is called, multiply the current value by 10, and add the button value to it.
  • and you can remove the step attribute as per the use case.
Sign up to request clarification or add additional context in comments.

Comments

0

In this function:

$(".normal-input").attr("value", $(this).attr('value'));

The second parameter is the value to set:

$(this).attr('value')

You need to have this as a combination of the previous value and the new value:

$(".normal-input").attr('value') + '' + $(this).attr('value')

The blank string is to make sure the final result is a string, not the addition of 2 numbers.

If you would like to convert it to a number, you can use parseInt():

const combinedNumber = $(".normal-input").attr('value') + '' + $(this).attr('value')
const intNumber = parseInt(combinedNumber)

The final code could look something like:

<input class="normal-input" type="number" step="0.01"> <!-- Value is NULL-->
<button value="7" class="my-buttons" type="button"> 7 </button> <!--Button to change the value of the input-->



$('.my-buttons').click(function(){

    const existingValue = $(".normal-input").attr('value')
    const newValue = $(this).attr('value'
    const combinedValue = parseInt(existingValue + '' + newValue)
    $(".normal-input").attr("value", combinedValue);

 }); <!-- The actual function. -->

2 Comments

Hello! I tried using this one but it does not quite work. I also modified it to this: $(".normal-input").attr('value', 'value' + $(this).attr('value')); Do you know what could happen?
I have edited the response to give a complete solution - please let me know if that works better
0

You just need to concat both the actual value and the value of button which is clicked to get required values .

Demo Code :

$('.my-buttons').click(function() {
  var value = $(".normal-input").val()
  var clicked_button = $(this).attr('value')
  //use val and combined both value
  $(".normal-input").val(value + "" + clicked_button);

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="normal-input" type="number">
<button value="7" class="my-buttons" type="button"> 7 </button>
<button value="10" class="my-buttons" type="button"> 10 </button>
<button value="6" class="my-buttons" type="button"> 6 </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.