Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!