Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

114
Visualizações
setAttribute not working consistently on change event

I'm having trouble getting a set of change events to work consistently: I have a table set up that checks to see whether either an input has been updated and then, depending on what input gets updated, the code submitted below either changes the margin or the price.

If I only change the price, I consistently calculate margin correctly, and if I only change the margin, I consistently calculate price correctly. If I change one and then the other, the second setAttribute doesn't work. (e.g. changing the price updates the margin, but, after that, changing margin, does not update price).

I'm new to JS, but I've tried debugging and can't seem to nail this down. Any help would be much appreciated.

see link for codepen: https://codepen.io/skeanerw/pen/mdBpppE

function calcBuySellMargin(bracket, rowID) {
    const vendPriceID = document.getElementById(`VendPrice_${bracket}_${rowID}`);
    const sellPriceID = document.getElementById(`ItemPrice_${bracket}_${rowID}`);
    const marginID = document.getElementById(`Margin_${bracket}_${rowID}`);
    const priceMeth = document.getElementById(`priceMeth_${rowID}`).innerText;
    const vpUOM = document.getElementById(`vpUOM_${rowID}`).innerText;

    //when we change the margin adjust the sell price.
    marginID.addEventListener('change', () => {
        let marginValue = (marginID.value) / 100
        let vendPriceValue = (vpUOM === 'M') ? vendPriceID.value / 1000 : vendPriceID.value;
        let sellPriceVal = (priceMeth === 'M') ? ((vendPriceValue / (1 - marginValue)) * 1000).toFixed(0) : (vendPriceValue / (1 - marginValue)).toFixed(3);
        sellPriceID.setAttribute('value', parseFloat(sellPriceVal));
    })

    //when we change the buy price or the sell price, adjust the margin.
    function setMargin() {
        let vendPriceValue = (vpUOM === 'M') ? vendPriceID.value / 1000 : vendPriceID.value;
        let sellPriceVal = (priceMeth === 'M') ? sellPriceID.value / 1000 : sellPriceID.value;
        const marginValue = parseFloat( (sellPriceVal - vendPriceValue) / sellPriceVal * 100 )
        marginID.setAttribute('value', parseFloat(marginValue).toFixed(0));
    }

    vendPriceID.addEventListener('change', () => {
        setMargin()
    })

    sellPriceID.addEventListener('change', () => {
        setMargin()
    })
}

window.onload = (event) => {
    document.querySelectorAll('.marginDisplay').forEach(element => {
        const rowID = (element.id.replace(/Margin_\d_/, ''));
        let bracket = (element.id.replace(/Margin_/,''))
        bracket = (bracket.match(/^[0-9]/, ''))
        calcBuySellMargin(bracket, rowID);
    })
}
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I was able to resolve by replacing the setAttribute methods:

    marginID.addEventListener('change', () => {
        let marginValue = (marginID.value) / 100
        let vendPriceValue = (vpUOM === 'M') ? vendPriceID.value / 1000 : vendPriceID.value;
        let sellPriceVal = (priceMeth === 'M') ? ((vendPriceValue / (1 - marginValue)) * 1000).toFixed(0) : (vendPriceValue / (1 - marginValue)).toFixed(3);
        sellPriceID.value = sellPriceVal;
    })

    function setMargin() {
        let vendPriceValue = (vpUOM === 'M') ? vendPriceID.value / 1000 : vendPriceID.value;
        let sellPriceVal = (priceMeth === 'M') ? sellPriceID.value / 1000 : sellPriceID.value;
        const marginValue = parseFloat( (sellPriceVal - vendPriceValue) / sellPriceVal * 100 )
        marginID.value = marginValue
    }
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda