I am a beginner in mvc and am developing an ecommerce website. In my cart list i need to enter specific quantity to calculate the total price. I used a java script call inside the foreach statement ,but the call only works for the first item in the cart itemlist.
when i click the second or other item's "Total Price" field then the controls goes to the first item's "Total Price" field.
My script is
<script>
function edValueKeyPress() {
var i ;
for (i = 0; i < 5; i++) {
var edValue = document.getElementById("qt");
var edValue1 = document.getElementById("cprice");
var s = edValue.value * edValue1.value;
var t = document.getElementById("total");
t.value = s;
}}
</script>
My view
<table class="table">
@foreach (var item in lscart)
{
<tr>
<td>
@{
var base64 = Convert.ToBase64String(item.CImage);
var imgSrc = String.Format("data:image/jpg;base64,{0}", base64);
<img src='@imgSrc' style="width:100px; height:100px;" />
}
</td>
<td>@Html.DisplayFor(modelItem => item.CProductName)</td>
<td>Rs.</td>
<td>@Html.EditorFor(modelItem => item.CPrice, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly",@id="cprice" } })</td>
<td>
<div class="col-md-6">
@Html.EditorFor(modelItem => item.Cquantity, new { htmlAttributes = new { @class = "form-control", @placeholder = " Qty",@id="qt" } })
</div>
</td>
<td>
<div>
@Html.EditorFor(modelItem => item.CTotalPrice, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", @placeholder = " Total Price", @id = "total", @onclick = "edValueKeyPress()", @onkeyup = "edValueKeyPress()" } })
</div>
</td>
<td><a href="#"><i class="fa fa-close"></i></a></td>
</tr>
}
</table>