Jump to content

how to calculate a percentage increase


Recommended Posts

Hi, how to calculate a percentage increase?
I have 3 fields to when entering a value in the second the third returns with the result.
I tried the code below, but nothing appears on preco2 field.
<div class="editor-field col-md-7">
@Html.TextBoxFor(model => model.preco1, new { id = "preco1", name = "preco1", @class = "form-control" })
</div>
<div class="editor-field col-md-7">
@Html.TextBoxFor(model => model.percentual, new { id = "percentual", name="percentual", @class = "form-control", placeholder = "1.00" })
</div>
<div class="editor-field col-md-7">
@Html.TextBoxFor(model => model.preco2, new { id = "preco2", name = "preco2", @class = "form-control" })
</div>
<script>
$("#percentual").change(function () {
preco= document.getElementById("#preco1").value;
percent = document.getElementById("#percentual").value;
document.getElementById("#preco2").innerHTML = preco * percent;
});
</script>
Edited by wrb
Link to post
Share on other sites

document.getElementById() does not use # to identify id, example preco= document.getElementById("#preco1").value; should be preco= document.getElementById("preco1").value;, also NEVER use .innerHTML for input value.

BUT! why not use jQuery to retrieve value

    $("#percentual").change(function () {              preco= $("#preco1").val();        percent = $("#percentual").val();            $("#preco2").val(preco * percent);        });
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...