function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Merve Bulut 8Merve Bulut 8 

how can i calculate total value in a table row with javascript in visualforce page?

i have a table with 3 column in visualforce page there is one input value and others are output value. When input value is changes, input value and second column value must be multiplied and the third column must show the result for per row. in table i'm using <apex:repeat> and <tr>, <td> tags so how can i do this? thanks.

All Answers

Shri RajShri Raj

Add an onchange attribute to the input field in each row, which calls a JavaScript function:
<td><input type="text" onchange="calculateTotal(this)" id="input_{!rowNum}" value="{!inputValue}"/></td>

Add id attribute to the other fields in each row, so that they can be accessed by JavaScript:
<td id="output_{!rowNum}">{!outputValue}</td>
<td id="result_{!rowNum}">{!result}</td>

In JavaScript function, you can use the id attribute of the input field to calculate the total value:
function calculateTotal(inputField) {
    var rowNum = inputField.id.split("_")[1];
    var inputValue = inputField.value;
    var outputValue = document.getElementById("output_" + rowNum).innerHTML;
    var result = inputValue * outputValue;
    document.getElementById("result_" + rowNum).innerHTML = result;
}

This code uses the id attribute of the input field to determine which row the input field belongs to, and then uses that information to access the other fields in the row and update their values based on the input value.