How to assign negative value in jQuery












0















I have a checkbox with two options in jQuery, withdrawal and deposit. When withdrawal is selected I want to assign negative to amount.The below link shows the image.



enter image description here










share|improve this question

























  • This might help you stackoverflow.com/questions/12769060/…

    – Manish Thapa
    Nov 25 '18 at 10:23






  • 2





    @ManishThapa What does CSS have to do with this

    – Barmar
    Nov 25 '18 at 10:23






  • 1





    amount = -1 * $("selector").val();

    – Barmar
    Nov 25 '18 at 10:24











  • Welcome to SO. Can you show us what you have tried so far? What happens when your user types in the amount after the transaction type has been selected?

    – Peter Smith
    Nov 25 '18 at 10:25
















0















I have a checkbox with two options in jQuery, withdrawal and deposit. When withdrawal is selected I want to assign negative to amount.The below link shows the image.



enter image description here










share|improve this question

























  • This might help you stackoverflow.com/questions/12769060/…

    – Manish Thapa
    Nov 25 '18 at 10:23






  • 2





    @ManishThapa What does CSS have to do with this

    – Barmar
    Nov 25 '18 at 10:23






  • 1





    amount = -1 * $("selector").val();

    – Barmar
    Nov 25 '18 at 10:24











  • Welcome to SO. Can you show us what you have tried so far? What happens when your user types in the amount after the transaction type has been selected?

    – Peter Smith
    Nov 25 '18 at 10:25














0












0








0








I have a checkbox with two options in jQuery, withdrawal and deposit. When withdrawal is selected I want to assign negative to amount.The below link shows the image.



enter image description here










share|improve this question
















I have a checkbox with two options in jQuery, withdrawal and deposit. When withdrawal is selected I want to assign negative to amount.The below link shows the image.



enter image description here







jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 10:23









Barmar

431k36254354




431k36254354










asked Nov 25 '18 at 10:19









user161379user161379

61




61













  • This might help you stackoverflow.com/questions/12769060/…

    – Manish Thapa
    Nov 25 '18 at 10:23






  • 2





    @ManishThapa What does CSS have to do with this

    – Barmar
    Nov 25 '18 at 10:23






  • 1





    amount = -1 * $("selector").val();

    – Barmar
    Nov 25 '18 at 10:24











  • Welcome to SO. Can you show us what you have tried so far? What happens when your user types in the amount after the transaction type has been selected?

    – Peter Smith
    Nov 25 '18 at 10:25



















  • This might help you stackoverflow.com/questions/12769060/…

    – Manish Thapa
    Nov 25 '18 at 10:23






  • 2





    @ManishThapa What does CSS have to do with this

    – Barmar
    Nov 25 '18 at 10:23






  • 1





    amount = -1 * $("selector").val();

    – Barmar
    Nov 25 '18 at 10:24











  • Welcome to SO. Can you show us what you have tried so far? What happens when your user types in the amount after the transaction type has been selected?

    – Peter Smith
    Nov 25 '18 at 10:25

















This might help you stackoverflow.com/questions/12769060/…

– Manish Thapa
Nov 25 '18 at 10:23





This might help you stackoverflow.com/questions/12769060/…

– Manish Thapa
Nov 25 '18 at 10:23




2




2





@ManishThapa What does CSS have to do with this

– Barmar
Nov 25 '18 at 10:23





@ManishThapa What does CSS have to do with this

– Barmar
Nov 25 '18 at 10:23




1




1





amount = -1 * $("selector").val();

– Barmar
Nov 25 '18 at 10:24





amount = -1 * $("selector").val();

– Barmar
Nov 25 '18 at 10:24













Welcome to SO. Can you show us what you have tried so far? What happens when your user types in the amount after the transaction type has been selected?

– Peter Smith
Nov 25 '18 at 10:25





Welcome to SO. Can you show us what you have tried so far? What happens when your user types in the amount after the transaction type has been selected?

– Peter Smith
Nov 25 '18 at 10:25












3 Answers
3






active

oldest

votes


















1














you can achieve that by input event on select input






$(function() {
const selectListEle = $('select');
selectListEle.on('input', function(e) {
const selectVal = selectListEle.val();
const inputVal = $('input').val();
if (selectVal === 'withdrawal') {
if (inputVal) {
$('input').val(-inputVal);
}
} else {
if (inputVal) {
$('input').val(-inputVal);
}
}
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>none</option>
<option>withdrawal</option>
</select>
<input type="number" value="">








share|improve this answer


























  • jQuery is javascript by the way so no harm at all to mixing between them the opposite sometimes you have to use vanilla javascript to achieve complex code implementation such array manipulating there's noway in jQuery to do that by jQuery you have to use vanilla js

    – Amir Fawzy
    Nov 25 '18 at 11:10











  • That's right, but this can be solved by using jQuery only, it's not necessary to mix native Javascript with jQuery functionalities at this point.

    – Gutelaunetyp
    Nov 25 '18 at 11:12











  • whay if twice? selectVal === 'withdrawal' because i want to make sure if the use choose withdrawal and if it's only add negative -- inputVal constant variable i would to avoid repetitive code $('input').val() -- what i used? again i want to make sure only if the input has value add negative with that implementation i strict the behavior to avoid any error may happening

    – Amir Fawzy
    Nov 25 '18 at 11:18













  • "jQuery is javascript by the way so no harm at all to mixing between them" - generally for DOM manipulation it's much better to be consistent. eg don't use e.target.value then $(e.target).val() - stick to one or the other.

    – freedomn-m
    Nov 25 '18 at 11:53





















0














Searching for $('input') in the whole DOM is a really bad idea.



Much cleaner:






$(function() {

$('.selectA').on('input', function(e) {
var selectedVal = $(e.currentTarget).val(),
$targetInput = $('.inputA'),
inputVal = $targetInput.val();
if (selectedVal === 'withdrawal' && inputVal.length) {
$targetInput.val(['-', inputVal].join(''));
}
});

});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="selectA">
<option>none</option>
<option>withdrawal</option>
</select>
<input class="inputA" type="number" value="">








share|improve this answer































    0














    One option is to give your select options a value of 1 for +ve and -1 for -ve, then you just multiple the two value together.



    $("#inp").val() * $("#opt").val()





    function calc() {
    $(this).nextAll("#result").text($("#inp").val() * $("#opt").val());
    }
    $("#inp").on("input", calc);
    $("#opt").on("change", calc);

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select id="opt">
    <option value="1">deposit</option>
    <option value="-1">withdrawal</option>
    </select>
    <input id="inp" type="number" value="" />
    <span id='result'></span>








    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466540%2fhow-to-assign-negative-value-in-jquery%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      you can achieve that by input event on select input






      $(function() {
      const selectListEle = $('select');
      selectListEle.on('input', function(e) {
      const selectVal = selectListEle.val();
      const inputVal = $('input').val();
      if (selectVal === 'withdrawal') {
      if (inputVal) {
      $('input').val(-inputVal);
      }
      } else {
      if (inputVal) {
      $('input').val(-inputVal);
      }
      }
      });
      });

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <select>
      <option>none</option>
      <option>withdrawal</option>
      </select>
      <input type="number" value="">








      share|improve this answer


























      • jQuery is javascript by the way so no harm at all to mixing between them the opposite sometimes you have to use vanilla javascript to achieve complex code implementation such array manipulating there's noway in jQuery to do that by jQuery you have to use vanilla js

        – Amir Fawzy
        Nov 25 '18 at 11:10











      • That's right, but this can be solved by using jQuery only, it's not necessary to mix native Javascript with jQuery functionalities at this point.

        – Gutelaunetyp
        Nov 25 '18 at 11:12











      • whay if twice? selectVal === 'withdrawal' because i want to make sure if the use choose withdrawal and if it's only add negative -- inputVal constant variable i would to avoid repetitive code $('input').val() -- what i used? again i want to make sure only if the input has value add negative with that implementation i strict the behavior to avoid any error may happening

        – Amir Fawzy
        Nov 25 '18 at 11:18













      • "jQuery is javascript by the way so no harm at all to mixing between them" - generally for DOM manipulation it's much better to be consistent. eg don't use e.target.value then $(e.target).val() - stick to one or the other.

        – freedomn-m
        Nov 25 '18 at 11:53


















      1














      you can achieve that by input event on select input






      $(function() {
      const selectListEle = $('select');
      selectListEle.on('input', function(e) {
      const selectVal = selectListEle.val();
      const inputVal = $('input').val();
      if (selectVal === 'withdrawal') {
      if (inputVal) {
      $('input').val(-inputVal);
      }
      } else {
      if (inputVal) {
      $('input').val(-inputVal);
      }
      }
      });
      });

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <select>
      <option>none</option>
      <option>withdrawal</option>
      </select>
      <input type="number" value="">








      share|improve this answer


























      • jQuery is javascript by the way so no harm at all to mixing between them the opposite sometimes you have to use vanilla javascript to achieve complex code implementation such array manipulating there's noway in jQuery to do that by jQuery you have to use vanilla js

        – Amir Fawzy
        Nov 25 '18 at 11:10











      • That's right, but this can be solved by using jQuery only, it's not necessary to mix native Javascript with jQuery functionalities at this point.

        – Gutelaunetyp
        Nov 25 '18 at 11:12











      • whay if twice? selectVal === 'withdrawal' because i want to make sure if the use choose withdrawal and if it's only add negative -- inputVal constant variable i would to avoid repetitive code $('input').val() -- what i used? again i want to make sure only if the input has value add negative with that implementation i strict the behavior to avoid any error may happening

        – Amir Fawzy
        Nov 25 '18 at 11:18













      • "jQuery is javascript by the way so no harm at all to mixing between them" - generally for DOM manipulation it's much better to be consistent. eg don't use e.target.value then $(e.target).val() - stick to one or the other.

        – freedomn-m
        Nov 25 '18 at 11:53
















      1












      1








      1







      you can achieve that by input event on select input






      $(function() {
      const selectListEle = $('select');
      selectListEle.on('input', function(e) {
      const selectVal = selectListEle.val();
      const inputVal = $('input').val();
      if (selectVal === 'withdrawal') {
      if (inputVal) {
      $('input').val(-inputVal);
      }
      } else {
      if (inputVal) {
      $('input').val(-inputVal);
      }
      }
      });
      });

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <select>
      <option>none</option>
      <option>withdrawal</option>
      </select>
      <input type="number" value="">








      share|improve this answer















      you can achieve that by input event on select input






      $(function() {
      const selectListEle = $('select');
      selectListEle.on('input', function(e) {
      const selectVal = selectListEle.val();
      const inputVal = $('input').val();
      if (selectVal === 'withdrawal') {
      if (inputVal) {
      $('input').val(-inputVal);
      }
      } else {
      if (inputVal) {
      $('input').val(-inputVal);
      }
      }
      });
      });

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <select>
      <option>none</option>
      <option>withdrawal</option>
      </select>
      <input type="number" value="">








      $(function() {
      const selectListEle = $('select');
      selectListEle.on('input', function(e) {
      const selectVal = selectListEle.val();
      const inputVal = $('input').val();
      if (selectVal === 'withdrawal') {
      if (inputVal) {
      $('input').val(-inputVal);
      }
      } else {
      if (inputVal) {
      $('input').val(-inputVal);
      }
      }
      });
      });

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <select>
      <option>none</option>
      <option>withdrawal</option>
      </select>
      <input type="number" value="">





      $(function() {
      const selectListEle = $('select');
      selectListEle.on('input', function(e) {
      const selectVal = selectListEle.val();
      const inputVal = $('input').val();
      if (selectVal === 'withdrawal') {
      if (inputVal) {
      $('input').val(-inputVal);
      }
      } else {
      if (inputVal) {
      $('input').val(-inputVal);
      }
      }
      });
      });

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <select>
      <option>none</option>
      <option>withdrawal</option>
      </select>
      <input type="number" value="">






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 25 '18 at 13:30

























      answered Nov 25 '18 at 10:34









      Amir FawzyAmir Fawzy

      24128




      24128













      • jQuery is javascript by the way so no harm at all to mixing between them the opposite sometimes you have to use vanilla javascript to achieve complex code implementation such array manipulating there's noway in jQuery to do that by jQuery you have to use vanilla js

        – Amir Fawzy
        Nov 25 '18 at 11:10











      • That's right, but this can be solved by using jQuery only, it's not necessary to mix native Javascript with jQuery functionalities at this point.

        – Gutelaunetyp
        Nov 25 '18 at 11:12











      • whay if twice? selectVal === 'withdrawal' because i want to make sure if the use choose withdrawal and if it's only add negative -- inputVal constant variable i would to avoid repetitive code $('input').val() -- what i used? again i want to make sure only if the input has value add negative with that implementation i strict the behavior to avoid any error may happening

        – Amir Fawzy
        Nov 25 '18 at 11:18













      • "jQuery is javascript by the way so no harm at all to mixing between them" - generally for DOM manipulation it's much better to be consistent. eg don't use e.target.value then $(e.target).val() - stick to one or the other.

        – freedomn-m
        Nov 25 '18 at 11:53





















      • jQuery is javascript by the way so no harm at all to mixing between them the opposite sometimes you have to use vanilla javascript to achieve complex code implementation such array manipulating there's noway in jQuery to do that by jQuery you have to use vanilla js

        – Amir Fawzy
        Nov 25 '18 at 11:10











      • That's right, but this can be solved by using jQuery only, it's not necessary to mix native Javascript with jQuery functionalities at this point.

        – Gutelaunetyp
        Nov 25 '18 at 11:12











      • whay if twice? selectVal === 'withdrawal' because i want to make sure if the use choose withdrawal and if it's only add negative -- inputVal constant variable i would to avoid repetitive code $('input').val() -- what i used? again i want to make sure only if the input has value add negative with that implementation i strict the behavior to avoid any error may happening

        – Amir Fawzy
        Nov 25 '18 at 11:18













      • "jQuery is javascript by the way so no harm at all to mixing between them" - generally for DOM manipulation it's much better to be consistent. eg don't use e.target.value then $(e.target).val() - stick to one or the other.

        – freedomn-m
        Nov 25 '18 at 11:53



















      jQuery is javascript by the way so no harm at all to mixing between them the opposite sometimes you have to use vanilla javascript to achieve complex code implementation such array manipulating there's noway in jQuery to do that by jQuery you have to use vanilla js

      – Amir Fawzy
      Nov 25 '18 at 11:10





      jQuery is javascript by the way so no harm at all to mixing between them the opposite sometimes you have to use vanilla javascript to achieve complex code implementation such array manipulating there's noway in jQuery to do that by jQuery you have to use vanilla js

      – Amir Fawzy
      Nov 25 '18 at 11:10













      That's right, but this can be solved by using jQuery only, it's not necessary to mix native Javascript with jQuery functionalities at this point.

      – Gutelaunetyp
      Nov 25 '18 at 11:12





      That's right, but this can be solved by using jQuery only, it's not necessary to mix native Javascript with jQuery functionalities at this point.

      – Gutelaunetyp
      Nov 25 '18 at 11:12













      whay if twice? selectVal === 'withdrawal' because i want to make sure if the use choose withdrawal and if it's only add negative -- inputVal constant variable i would to avoid repetitive code $('input').val() -- what i used? again i want to make sure only if the input has value add negative with that implementation i strict the behavior to avoid any error may happening

      – Amir Fawzy
      Nov 25 '18 at 11:18







      whay if twice? selectVal === 'withdrawal' because i want to make sure if the use choose withdrawal and if it's only add negative -- inputVal constant variable i would to avoid repetitive code $('input').val() -- what i used? again i want to make sure only if the input has value add negative with that implementation i strict the behavior to avoid any error may happening

      – Amir Fawzy
      Nov 25 '18 at 11:18















      "jQuery is javascript by the way so no harm at all to mixing between them" - generally for DOM manipulation it's much better to be consistent. eg don't use e.target.value then $(e.target).val() - stick to one or the other.

      – freedomn-m
      Nov 25 '18 at 11:53







      "jQuery is javascript by the way so no harm at all to mixing between them" - generally for DOM manipulation it's much better to be consistent. eg don't use e.target.value then $(e.target).val() - stick to one or the other.

      – freedomn-m
      Nov 25 '18 at 11:53















      0














      Searching for $('input') in the whole DOM is a really bad idea.



      Much cleaner:






      $(function() {

      $('.selectA').on('input', function(e) {
      var selectedVal = $(e.currentTarget).val(),
      $targetInput = $('.inputA'),
      inputVal = $targetInput.val();
      if (selectedVal === 'withdrawal' && inputVal.length) {
      $targetInput.val(['-', inputVal].join(''));
      }
      });

      });

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <select class="selectA">
      <option>none</option>
      <option>withdrawal</option>
      </select>
      <input class="inputA" type="number" value="">








      share|improve this answer




























        0














        Searching for $('input') in the whole DOM is a really bad idea.



        Much cleaner:






        $(function() {

        $('.selectA').on('input', function(e) {
        var selectedVal = $(e.currentTarget).val(),
        $targetInput = $('.inputA'),
        inputVal = $targetInput.val();
        if (selectedVal === 'withdrawal' && inputVal.length) {
        $targetInput.val(['-', inputVal].join(''));
        }
        });

        });

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <select class="selectA">
        <option>none</option>
        <option>withdrawal</option>
        </select>
        <input class="inputA" type="number" value="">








        share|improve this answer


























          0












          0








          0







          Searching for $('input') in the whole DOM is a really bad idea.



          Much cleaner:






          $(function() {

          $('.selectA').on('input', function(e) {
          var selectedVal = $(e.currentTarget).val(),
          $targetInput = $('.inputA'),
          inputVal = $targetInput.val();
          if (selectedVal === 'withdrawal' && inputVal.length) {
          $targetInput.val(['-', inputVal].join(''));
          }
          });

          });

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <select class="selectA">
          <option>none</option>
          <option>withdrawal</option>
          </select>
          <input class="inputA" type="number" value="">








          share|improve this answer













          Searching for $('input') in the whole DOM is a really bad idea.



          Much cleaner:






          $(function() {

          $('.selectA').on('input', function(e) {
          var selectedVal = $(e.currentTarget).val(),
          $targetInput = $('.inputA'),
          inputVal = $targetInput.val();
          if (selectedVal === 'withdrawal' && inputVal.length) {
          $targetInput.val(['-', inputVal].join(''));
          }
          });

          });

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <select class="selectA">
          <option>none</option>
          <option>withdrawal</option>
          </select>
          <input class="inputA" type="number" value="">








          $(function() {

          $('.selectA').on('input', function(e) {
          var selectedVal = $(e.currentTarget).val(),
          $targetInput = $('.inputA'),
          inputVal = $targetInput.val();
          if (selectedVal === 'withdrawal' && inputVal.length) {
          $targetInput.val(['-', inputVal].join(''));
          }
          });

          });

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <select class="selectA">
          <option>none</option>
          <option>withdrawal</option>
          </select>
          <input class="inputA" type="number" value="">





          $(function() {

          $('.selectA').on('input', function(e) {
          var selectedVal = $(e.currentTarget).val(),
          $targetInput = $('.inputA'),
          inputVal = $targetInput.val();
          if (selectedVal === 'withdrawal' && inputVal.length) {
          $targetInput.val(['-', inputVal].join(''));
          }
          });

          });

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <select class="selectA">
          <option>none</option>
          <option>withdrawal</option>
          </select>
          <input class="inputA" type="number" value="">






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 10:57









          GutelaunetypGutelaunetyp

          15010




          15010























              0














              One option is to give your select options a value of 1 for +ve and -1 for -ve, then you just multiple the two value together.



              $("#inp").val() * $("#opt").val()





              function calc() {
              $(this).nextAll("#result").text($("#inp").val() * $("#opt").val());
              }
              $("#inp").on("input", calc);
              $("#opt").on("change", calc);

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <select id="opt">
              <option value="1">deposit</option>
              <option value="-1">withdrawal</option>
              </select>
              <input id="inp" type="number" value="" />
              <span id='result'></span>








              share|improve this answer




























                0














                One option is to give your select options a value of 1 for +ve and -1 for -ve, then you just multiple the two value together.



                $("#inp").val() * $("#opt").val()





                function calc() {
                $(this).nextAll("#result").text($("#inp").val() * $("#opt").val());
                }
                $("#inp").on("input", calc);
                $("#opt").on("change", calc);

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <select id="opt">
                <option value="1">deposit</option>
                <option value="-1">withdrawal</option>
                </select>
                <input id="inp" type="number" value="" />
                <span id='result'></span>








                share|improve this answer


























                  0












                  0








                  0







                  One option is to give your select options a value of 1 for +ve and -1 for -ve, then you just multiple the two value together.



                  $("#inp").val() * $("#opt").val()





                  function calc() {
                  $(this).nextAll("#result").text($("#inp").val() * $("#opt").val());
                  }
                  $("#inp").on("input", calc);
                  $("#opt").on("change", calc);

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <select id="opt">
                  <option value="1">deposit</option>
                  <option value="-1">withdrawal</option>
                  </select>
                  <input id="inp" type="number" value="" />
                  <span id='result'></span>








                  share|improve this answer













                  One option is to give your select options a value of 1 for +ve and -1 for -ve, then you just multiple the two value together.



                  $("#inp").val() * $("#opt").val()





                  function calc() {
                  $(this).nextAll("#result").text($("#inp").val() * $("#opt").val());
                  }
                  $("#inp").on("input", calc);
                  $("#opt").on("change", calc);

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <select id="opt">
                  <option value="1">deposit</option>
                  <option value="-1">withdrawal</option>
                  </select>
                  <input id="inp" type="number" value="" />
                  <span id='result'></span>








                  function calc() {
                  $(this).nextAll("#result").text($("#inp").val() * $("#opt").val());
                  }
                  $("#inp").on("input", calc);
                  $("#opt").on("change", calc);

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <select id="opt">
                  <option value="1">deposit</option>
                  <option value="-1">withdrawal</option>
                  </select>
                  <input id="inp" type="number" value="" />
                  <span id='result'></span>





                  function calc() {
                  $(this).nextAll("#result").text($("#inp").val() * $("#opt").val());
                  }
                  $("#inp").on("input", calc);
                  $("#opt").on("change", calc);

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <select id="opt">
                  <option value="1">deposit</option>
                  <option value="-1">withdrawal</option>
                  </select>
                  <input id="inp" type="number" value="" />
                  <span id='result'></span>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '18 at 11:50









                  freedomn-mfreedomn-m

                  13k31945




                  13k31945






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466540%2fhow-to-assign-negative-value-in-jquery%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin