0

I am having trouble getting one of my buttons to append data to a HTML text area. The button in question is "BPI". I am trying to append a few digits of pi to my first text area. The other 4 buttons operate within the 2nd text area. Thank you in advance for your help.

$(document).ready(function() {
  $('#BAddition').click(function(e) {
    $('#TOperator').val() += "+";

  })

  $('#BSubtract').click(function(e) {
    $('#TOperator').val() += "-";
  })

  $('#BMultiplication').click(function(e) {
    $('#TOperator').val() += "*";
  })

  $('#BDivision').click(function(e) {
    $('#TOperator').val() += "/";
  })

  $('#BPI').click(function(e) {
    $('#TFirstnum').val() += "3.141592657";
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Input:<br>
<textarea id="TFirstnum"></textarea>
<textarea id="TOperator"></textarea>
<textarea id="TSecondnum"></textarea>
<label id="LEquals"></label>
<br>
<button type="button" onclick="" id="BAddition">+</button>
<button type="button" onclick="" id="BSubtract">-</button>
<button type="button" onclick="" id="BDivision">/</button>
<button type="button" onclick="" id="BMultiplication">*</button>
<button type="button" onclick="" id="BPI">&#960;</button>
<button type="button" onclick="" id="BEquals">=</button>

1
  • 2
    try $('#TOperator').val($('#TOperator').val() + "+") Commented Aug 17, 2017 at 11:33

4 Answers 4

1

If you want to add a value use $(yourObject).val("yourvalue")

If you want to add a value to the already existing value use $(yourObject).val($(yourObject).val() + "yourvalue")

Working example below

$(document).ready(function() {
  $('#BAddition').click(function(e) {
    $('#TOperator').val($('#TOperator').val() + "+");

  })

  $('#BSubtract').click(function(e) {
    $('#TOperator').val($('#TOperator').val() + "-");
  })

  $('#BMultiplication').click(function(e) {
    $('#TOperator').val($('#TOperator').val() + "*");
  })

  $('#BDivision').click(function(e) {
    $('#TOperator').val($('#TOperator').val() + "/");
  })

  $('#BPI').click(function(e) {

    $('#TOperator').val($('#TOperator').val() + "3.141592657");
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Input:<br>
<textarea id="TFirstnum"></textarea>
<textarea id="TOperator"></textarea>
<textarea id="TSecondnum"></textarea>
<label id="LEquals"></label>
<br>
<button type="button" onclick="" id="BAddition">+</button>
<button type="button" onclick="" id="BSubtract">-</button>
<button type="button" onclick="" id="BDivision">/</button>
<button type="button" onclick="" id="BMultiplication">*</button>
<button type="button" onclick="" id="BPI">&#960;</button>
<button type="button" onclick="" id="BEquals">=</button>

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

Comments

0

When you want to set an element's value attribute using Jquery, you need to set the value inside the parantheses: .val('newValue').

When you want to get an element's value, use blank parantheses: var x = elem.val().

So to concatenate (or append) values, you should use:

$('#TOperator').val($('#TOperator').val() + '+');

A longer version to elaborate:

var expr = $('#TOperator').val();  // Get current value
expr += '+';                       // Concatenate the + sign
$('#TOperator').val(expr);         // Set the new value

1 Comment

Are you getting an error? What is the expected result when you click this button? please try to explain.
0

You are useing += which does not work. You have to get the value, and then set the value;

$('#BAddition').click(function(e) {
    $('#TOperator').val( $('#TOperator').val() + "+");
});

You're repeating yourself a lot. I suggest you change the html a bit and create a much neater function.

<button type="button" class="MathSign" data-sign="+">+</button>
<button type="button" class="MathSign" data-sign="-">-</button>
<button type="button" class="MathSign" data-sign="/">/</button>
<button type="button" class="MathSign" data-sign="*">*</button>

<button type="button" id="BPI">&#960;</button>
<button type="button" class="MathSign" data-sign="=">=</button>


// handle the [+ - * / =] in one go:
$('.MathSign').on('click', function(){
    $('#TOperator').val( $('#TOperator').val() + $(this).data('sign'));

    // Or,you can drop the data-sign attribute and use the content of the button
    $('#TOperator').val( $('#TOperator').val() + this.innerHTML);
})                                                              

5 Comments

don't you have 1 " too much in data-sign="=""
Thanks, fixed it :)
Not a problem :)
I didn't think of using HTML class attributes to be able to process the inputs of several buttons at once. Thanks you for helping me improve my program more :)
Thats what we're for :) Just upvote the answers you like and maark the answer you think helped you most as answer
0

$(selector).val() is used to get the value of the input box.

If you want to set the value, you need to pass the value $(selector).val(value).

I have modified your example. One thing you need to consider is when you do $(selector).val(), you will get string. If you want to have addition instead of concatenation of two strings, You should probably use parseInt

$(document).ready(function() {
  $('#BAddition').click(function(e) {
    $('#TOperator').val($('#TOperator').val() + "+");
  })

  $('#BSubtract').click(function(e) {
    $('#TOperator').val($('#TOperator').val() + "-");
  })

  $('#BMultiplication').click(function(e) {
    $('#TOperator').val($('#TOperator').val() + "*");
  })

  $('#BDivision').click(function(e) {
    $('#TOperator').val($('#TOperator').val() + "/");
  })

  $('#BPI').click(function(e) {
    $('#TFirstnum').val($('#TFirstnum').val() + "3.141592657");
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Input:<br>
<textarea id="TFirstnum"></textarea>
<textarea id="TOperator"></textarea>
<textarea id="TSecondnum"></textarea>
<label id="LEquals"></label>
<br>
<button type="button" onclick="" id="BAddition">+</button>
<button type="button" onclick="" id="BSubtract">-</button>
<button type="button" onclick="" id="BDivision">/</button>
<button type="button" onclick="" id="BMultiplication">*</button>
<button type="button" onclick="" id="BPI">&#960;</button>
<button type="button" onclick="" id="BEquals">=</button>

1 Comment

I edited only the BPI button. Although I have edited other buttons also now.

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.