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

133
Visualizações
Initial load and when I change value of input doesn't show message in JavaScript

everyone, I have some problem with fetching data and displaying message on initial loading as well as when I change some of the input filed value. The idea here is to display specific message in two cases after doing some calculation.

const potrosnja = document.getElementById('potrosnja');
const nagib = document.getElementById('nagib');
const input = document.querySelectorAll('input[type="number"]');
const submitBtn = document.getElementById('submitBtn');
const poruka = document.getElementById('poruka');
let str = document.querySelector('input[name="strane-sveta"]:checked').value;

let godisnjaPotrosnja = parseInt(potrosnja.value);
let nagibKrovaInput = nagib.value;

//On button submit it fetches data and calculate the value needed for yearly consumption of energy 

//fetching data

async function dataFetch(){
    let response = await fetch('./csvjson.json')
    let data = await response.json();
    data.map(strana => {
        strana.strana.map((item, i) => {
            try {
                if(item == str && nagibKrovaInput == strana.nagib) {
                    let result = Math.ceil(godisnjaPotrosnja / strana.vrednost[i]);
                    console.log("try works")
                    poruka.innerHTML = `You need <span class="kw">${result}</span>`
                }
             }
             catch(err) {
                poruka.innerHTML = `Please fill required fields.`
                console.log(err)
            }
        })
    })
}

//event listeners

submitBtn.addEventListener('click', () => {    
    dataFetch()
    console.log('clicked')
    input.forEach(input => {
        if(input.value == ''){
            input.classList.add("active");
        }
    })
})

I can see that the problem is inside try function, it like condition isn't treated on initial load, and I have to reload page so it would work. Can someone help me understanding what is the problem?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Ok, I found solution... First thing I have to do Is to check if nagibKrovaInput == strana.nagib, after I get true, I compared does the indexOf item is equal as str and after I get true, it will display something. I also changed on click on the button to send values to data function as an arguments and It solved the problem. Tnx for help.

about 4 years ago · Juan Pablo Isaza 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