Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
1.7 kB
5
Indexable
Never
// Get the necessary elements by their IDs or classes
const moneyInput = document.getElementById('moneyAmount');
const moneyElement = document.querySelector('.money');
const creditTransDivs = document.querySelectorAll('.credit-trans');
const addButton = document.querySelector('.add-money-btn');

// Add a click event listener to the button
addButton.addEventListener('click', function() {
    // Get the value from the input element
    const inputValue = moneyInput.value;

    // Update the text content of the money element with the input value
    moneyElement.textContent = inputValue;

    // Find the selected credit-trans div and get its text content
    let creditValue = '';
    creditTransDivs.forEach(div => {
        if (div.classList.contains('selected')) {
            creditValue = div.textContent.trim();
        }
    });

    // Update the text content of the creditSpan with the credit value
    creditSpan.textContent = creditValue;
});

// Function to handle updating input value and selecting the credit-trans div
function updateInputValue(value) {
    // Remove the 'selected' class from all credit-trans divs
    creditTransDivs.forEach(div => {
        div.classList.remove('selected');
    });

    // Find the credit-trans div with the corresponding value
    const selectedDiv = Array.from(creditTransDivs).find(div => {
        return div.textContent.trim().includes(`${value} credits tương ứng`);
    });

    // Add the 'selected' class to the selected div
    if (selectedDiv) {
        selectedDiv.classList.add('selected');
    }

    // Update the value of the money input
    moneyInput.value = value;
}