0

I'm currently working on a weather site. I want to get the user input into a variable that changes the current city. Hope someone of you can help me.

var userInput = "London";

function changeValue() {
  userInput = document.getElementById("user_Input").value;
  document.getElementById("location").innerHTML = document.getElementById("user_Input").value;
}

window.onload = function() {

var api = "http://api.openweathermap.org/data/2.5/forecast/daily?q=";
var units = "&units=metric";
var url2 = api + userInput + units + APPID;

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if(this.readyState == 4 && this.status == 200) {
        myObj = JSON.parse(this.responseText);
        
        var iconCode = myObj.list[0].weather[0].id;
        var directions = myObj.list[0].deg;

        for (var i = 0; i < 7; i++) {
            
            document.getElementById("day" + i).innerHTML =  Math.round(myObj.list[i].temp.day) + "°";
            document.getElementById("icon" + i).src = "sl_line/" + iconCode + ".svg";
            document.getElementById("wota" + i).innerHTML = wochentag[day.getDay()+i+1];
            document.getElementById("humidity").innerHTML = Math.round(myObj.list[0].humidity);
            document.getElementById("wind").innerHTML = myObj.list[0].speed + " m/s";
            document.getElementById("direction").innerHTML = directions;
        }
    }
};

xmlhttp.open("GET", url2, true);
xmlhttp.send();
};
<article>
  <input id="user_Input" type="text" placeholder="Ortsuchen...">
  <button id="submit" onclick="changeValue()">Submit</button>
  <span id="location">Unknown</span>
</article>
<section>
      <div>
          <div><span id="day0" class="ml:.25r fs:6r">0</span></div>
          <div><span id="wochentage">0</span></div>
      </div>
      <div>
          <img id="icon0">
      </div>
      <div>
          <span id="wind">0</span><span id="direction"></span><span></span>
      </div>
      <div>
          <span id="humidity">0</span><span>%</span>
      </div>
</section>

location is getting updated after hitting the submit button. But the value of the variable doesn't change. I've already looked around stackoverflow and other sites but didn't find any solution that works for me.

6
  • 1
    How are you validating that the value of variable has not changes? Commented Jun 30, 2017 at 11:13
  • its properly work whats wrong in this.. Commented Jun 30, 2017 at 11:14
  • Your code works. what is the issue? Commented Jun 30, 2017 at 11:17
  • After page reload again the variable set to var userInput = "London"; Commented Jun 30, 2017 at 11:30
  • Don't push the code in comment . just update your question using edit option @DeputyDong_ Commented Jun 30, 2017 at 11:31

1 Answer 1

1

1st : Your request will only run on page load . you need to run that again after user change the userInput .so make it as a function and call it every time of userInput change like this .

var userInput = "London";

function changeValue() {
  userInput = document.getElementById("user_Input").value;
  document.getElementById("location").innerHTML = document.getElementById("user_Input").value;
  
  ajax_call();
}

function ajax_call(){

var api = "http://api.openweathermap.org/data/2.5/forecast/daily?q=";
var units = "&units=metric";
var url2 = api + userInput + units + APPID;

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if(this.readyState == 4 && this.status == 200) {
        myObj = JSON.parse(this.responseText);
        
        var iconCode = myObj.list[0].weather[0].id;
        var directions = myObj.list[0].deg;

        for (var i = 0; i < 7; i++) {
            
            document.getElementById("day" + i).innerHTML =  Math.round(myObj.list[i].temp.day) + "°";
            document.getElementById("icon" + i).src = "sl_line/" + iconCode + ".svg";
            document.getElementById("wota" + i).innerHTML = wochentag[day.getDay()+i+1];
            document.getElementById("humidity").innerHTML = Math.round(myObj.list[0].humidity);
            document.getElementById("wind").innerHTML = myObj.list[0].speed + " m/s";
            document.getElementById("direction").innerHTML = directions;
        }
    }
};

xmlhttp.open("GET", url2, true);
xmlhttp.send();

}

window.onload = function() {

    ajax_call();
};
<article>
  <input id="user_Input" type="text" placeholder="Ortsuchen...">
  <button id="submit" onclick="changeValue()">Submit</button>
  <span id="location">Unknown</span>
</article>
<section>
      <div>
          <div><span id="day0" class="ml:.25r fs:6r">0</span></div>
          <div><span id="wochentage">0</span></div>
      </div>
      <div>
          <img id="icon0">
      </div>
      <div>
          <span id="wind">0</span><span id="direction"></span><span></span>
      </div>
      <div>
          <span id="humidity">0</span><span>%</span>
      </div>
</section>

Sign up to request clarification or add additional context in comments.

1 Comment

if my answer is useful mark it with green tick it's useful for future user reference .@DeputyDong_

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.