-3
<div id="minus1">
    <input id="minus1str_1" class="input_box"> -
    <input id="minus1str_2" class="input_box"> =
    <span id="minus1_total"></span>
    <button id="m_cal1" type="button" style="margin-left:100px;">done</button>
</div>

if minus1str_1 enter "abcde" and minus1str_2 enter "abc" minus1_total = "de"

I want to know how to create code with jQuery.

1

1 Answer 1

2

You can use replace() which will replace the string with '' so that it will simulate as if it has been subtracted:

$('#m_cal1').click(function(){
  var minus1str_1 = $('#minus1str_1').val();
  var minus1str_2 = $('#minus1str_2').val();
  $('#minus1_total').html(minus1str_1.replace(minus1str_2,''));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="minus1">
    <input id="minus1str_1" class="input_box"> -
    <input id="minus1str_2" class="input_box"> =
    <span id="minus1_total"></span>
    <button id="m_cal1" type="button" style="margin-left:100px;">done</button>
</div>

If you want to subtract all the occurrences then use global replace like:

$('#m_cal1').click(function(){
  var minus1str_1 = $('#minus1str_1').val();
  var minus1str_2 = $('#minus1str_2').val();
  $('#minus1_total').html(minus1str_1.replace(new RegExp(minus1str_2, 'g'),''));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="minus1">
    <input id="minus1str_1" class="input_box"> -
    <input id="minus1str_2" class="input_box"> =
    <span id="minus1_total"></span>
    <button id="m_cal1" type="button" style="margin-left:100px;">done</button>
</div>

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

9 Comments

thank you very much !!!!
@ctmong glad to help :)
Can I ask you one more question?
@ctmong yes sure please. If its a bigger question you can create a new question
I wanna know what to do if one more input is added to the code I asked ex) string - string - string / add: var minus1str_3 = $('#minus1str_3').val();
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.