I have below lines of code in asp:list view control, if user input any value on 2nd text box, on client side lost focus event I need to calculate txtTokensReq * txtQty and display in lblTotalTokens.
WHen I wrote onblur function on 2nd textbox, it is firing more than once.
Thanks in advance.
Code
' ReadOnly="true">
'
onKeypress ="if(!validatenumeric(this,5)){return false;}">
'
runat="server">
Naimish MakwanaRank #324 · 7.7K reputationPosted May 27, 2024, 1:19 AM EDT
Certainly! To achieve the desired behavior of calculating
txtTokensReq * txtQtyand displaying the result inlblTotalTokenswhen the user inputs a value in the second textbox and loses focus, you can follow these steps:Client-Side Event Handling:
onblurevent directly on the second textbox, we’ll attach an event listener to it.JavaScript Implementation:
txtQty) in your JavaScript code.txtQtytextbox for theblurevent.txtTokensReqandtxtQty, and update thelblTotalTokensaccordingly.Here’s an example of how you can achieve this:
// Assuming you have a reference to the second textbox (txtQty) in your JavaScript code var txtQty = document.getElementById("txtQty"); // Attach an event listener for the blur event txtQty.addEventListener("blur", function () { // Get the values from the textboxes var tokensReq = parseFloat(document.getElementById("txtTokensReq").value); var qty = parseFloat(txtQty.value); // Calculate the total tokens var totalTokens = tokensReq * qty; // Update the lblTotalTokens document.getElementById("lblTotalTokens").innerText = totalTokens; });txtTokensReq,txtQty, andlblTotalTokens) match the actual IDs in your markup.Remember to adjust the code according to your specific scenario and any additional requirements you might have. If you encounter any issues or need further assistance, feel free to ask! ??
References:
Thanks
Jayraj ChhayaRank #304 · 8.2K reputationPosted May 27, 2024, 2:02 AM EDT
To address the multiple firing of the onblur event in the second textbox, you can ensure that the event is attached only once. One way to achieve this is by using jQuery to bind the event.
$(document).ready(function () { $('#').on('blur', function () { var tokensReq = parseInt($('#').val()); var qty = parseInt($(this).val()); var totalTokens = tokensReq * qty; $('#').text(totalTokens); }); });By binding the blur event using jQuery in this manner, you can ensure that the event is attached only once, preventing multiple firing issues. This approach should help you calculate the total tokens and display the result accurately in the
lblTotalTokenslabel on the client-side.Mohan30 reputationPosted May 27, 2024, 3:58 PM EDT
Thank you Namish & Jayraj, but the main question here is how to get the reference to the row when a user enters the Quantity and click on Tab?
Prasad RaveendranRank #320 · 7.8K reputationPosted May 28, 2024, 8:37 PM EDT
To achieve this, you can use JavaScript to handle the
onblurevent of the secondTextBox(txtQty) and calculate the total tokens required. Then, update the correspondingLabel(lblTotalTokens) within the same row of theListView. Here is a step-by-step solution:onblurEvent Handler totxtQty: Attach anonblurevent totxtQtyto trigger the calculation when the user leaves the textbox.txtQty.lblTotalTokens: Multiply the values oftxtTokensReqandtxtQty, then update thelblTotalTokenswith the result.Here is the updated ASP.NET code with the JavaScript function:
' ReadOnly="true"> ' onblur="calculateTotalTokens(this)"> ' runat="server"> function calculateTotalTokens(txtQtyElement) { // Get the parent row of the txtQtyElement var parentRow = txtQtyElement.closest('.row'); // Get the txtTokensReq input element in the same row var txtTokensReq = parentRow.querySelector('#txtTokensReq'); // Get the lblTotalTokens label element in the same row var lblTotalTokens = parentRow.querySelector('#lblTotalTokens'); // Calculate the total tokens required var tokensReq = parseFloat(txtTokensReq.value) || 0; var qty = parseFloat(txtQtyElement.value) || 0; var totalTokensReq = tokensReq * qty; // Update the lblTotalTokens with the calculated value lblTotalTokens.textContent = totalTokensReq.toFixed(2); }Explanation:
onblurevent is added directly totxtQtyusing theonblur="calculateTotalTokens(this)"attribute.calculateTotalTokens):calculateTotalTokensfunction receives thetxtQtyelement that triggered the event.closest('.row').txtTokensReqandlblTotalTokenselements usingquerySelector.txtTokensReqandtxtQty, converts them to numbers, performs the multiplication, and updateslblTotalTokenswith the result.This solution ensures that the calculation is performed correctly and only once per user interaction. The use of
closestandquerySelectorensures that the JavaScript code is focused on the specific row where the event was triggered, thus avoiding any potential issues with duplicate control IDs.Tuhin PaulRank #7 · 71.9K reputationPosted May 30, 2024, 10:29 PM EDT
To get a reference to the row when a user enters the Quantity and clicks on Tab, you can utilize event delegation. Event delegation allows you to attach an event listener to a parent element (like a table) and then handle events for its child elements (like rows or cells). This way, you can dynamically determine which row the event originated from.
Tuhin PaulRank #7 · 71.9K reputationPosted May 30, 2024, 10:30 PM EDT
// Assuming you have a reference to the table element with ID "myTable" in your JavaScript code var table = document.getElementById("myTable"); // Attach an event listener for the keydown event on the table table.addEventListener("keydown", function (event) { // Check if the pressed key is the Tab key if (event.key === "Tab") { // Get the target element that triggered the event var target = event.target; // Check if the target element is an input field and has a class or ID to identify it as the Quantity input field if (target.tagName === "INPUT" && target.classList.contains("quantity-input")) { // Retrieve the row identifier from the closest parent row var row = target.closest("tr"); var rowId = row.dataset.rowId; // Assuming you have a data attribute named "rowId" // Perform further processing based on the row identifier // For example, update the Total Tokens label for the corresponding row updateTotalTokens(rowId); } } }); // Function to update the Total Tokens label for the specified row function updateTotalTokens(rowId) { // Retrieve the Quantity and Tokens Required values from the input fields in the specified row var quantity = parseFloat(document.querySelector(`tr[data-row-id="${rowId}"] .quantity-input`).value); var tokensReq = parseFloat(document.querySelector(`tr[data-row-id="${rowId}"] .tokens-req-input`).value); // Calculate the Total Tokens var totalTokens = quantity * tokensReq; // Update the Total Tokens label for the corresponding row document.querySelector(`tr[data-row-id="${rowId}"] .total-tokens-label`).textContent = totalTokens; }assume that each row in your table has a unique identifier stored in a data attribute (e.g., data-row-id). Adjust the code according to your specific HTML structure and requirements.