Calculate via JS by Span id

Asked

Viewed 43 times

0

In the code below, the two are 'fed' by a select via JS, i.e., I choose the option on select and it fills me these two fields (numerical, Float).

I have another field input which is 'powered' by other inputs (sum of inputs, total shopping cart).

What I want to do is this: when to change the input "total" or when changing the "aaa" or "bbb", via JS, happen a type operation: "total" * "aaa" * "bbb" and fills the input "ccc".

<input name="total" id="total" hidden/>

<select name="forma_pgto" id="forma_pgto">				
  <option value="1">A Vista</option>
  <option value="2">Credito</option>
</select>

<span name="aaa" id="aaa" hidden>0</span>
<span name="bbb" id="bbb" hidden>0</span>

<input name="ccc" id="ccc" readonly/>

The selection of select makes a real-time search in the BD and returns the value of the 2 rates to "aaa" and "bbb".

I tried it like this, but it didn’t work:

$(function(){
   var taxa1 = Number(document.getElementById("aaa").value);
   var taxa2 = Number(document.getElementById("bbb").value);
   var total = Number(document.getElementById("total").value);
   $("#forma_pgto").on("change", function(){ //usando o id do select
	$("#ccc").val(taxa1.toFixed(2)*taxa2.toFixed(2)*total.toFixed(2));
		});
});

EDIT - to illustrate my operation, I made the table as per: Mark Checklist container and run JS

And this select forma_pgto, would modify the total value of the cart (because it depends on card fees, so if it is the view the rate is X, credit Y and so on...) and would throw the final value in the "ccc".

  • 1

    When aaa, bbb, or total are modified, nothing will happen, because you have applied the on change to the forma_pgto tag. Only when forma_pgto is modified, values will be updated.

  • Well, thank you for the answer. However, even the multiplication operation is not working, I believe you are not getting the values of the <span>. The value of the <input> "total" and the <span> "aaa" and <span> "bbb" can be changed with some frequency and when changed would like to update the <span> "ccc" with the operation described above.

1 answer

1


For starters, make sure that jQuery is imported into your page...

Cup the next:

$("#aaa").on("change", function() {
   Recalcular();
});

$("#bbb").on("change", function() {
   Recalcular();
});

$("#total").on("change", function() {
   Recalcular();
});

function Recalcular() {
   var taxa1 = Number(document.getElementById("aaa").textContent);
   var taxa2 = Number(document.getElementById("bbb").textContent);
   var total = Number(document.getElementById("total").value);
   $("#ccc").val(taxa1.toFixed(2)*taxa2.toFixed(2)*total.toFixed(2));
}
  • The multiplication operation does not occur and the "ccc" input remains empty... did I make sure how to pass span’s values "aaa" and "bbb"? remember that this two span’s and the total input are filled via JS tb (previously)

  • 1

    I changed the answer, because the span comes via textContent and not by value. try this. I see the console and tell me.

  • No chance. I change the products and the "total" updates normally. I switch the select "forma_pgto" and the two span (aaa and bbb) update, but the ccc operation does not occur. I switched to <span> for <input>, but I didn’t...switch back to <span>

  • 1

    Anything that is not for the user to move hold as span. Change the ccc to span and take the readyonly. can be it the impediment of altetraction...

  • Nothing. It’s very strange, I don’t understand why you’re not passing the values of the id’s. I changed the code to illustrate what I want to do.

  • 1

    Go to the chat room, "Luis Help you"...

  • I appreciate your commitment to help. But I still can’t make it work.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.