How do I update the content of body instead of adding to existing content?

kasra nejad

I'm creating a simple practice weather page where you get weather data for a particular city.

Currently when I enter a city it displays the data by creating ul/li elements, but when I enter a second city instead of replacing and updating the existing elements, it just adds the data for the second city under the first city.

window.onload = function() {
  let cityName = document.querySelector("#city");
  let button = document.querySelector("#submit");
  // Checks value type and displays property and value.
  let logLi = function(k, j) {
    if (typeof j === 'object') {
      isObject(j);
    } else {
      let li = document.createElement('LI');
      let liText = document.createTextNode('\u00A0\u00A0\u00A0\u00A0' + k + ": " + j);
      li.appendChild(liText)
      document.querySelector('body').appendChild(li);
    }
  }
  // Checks Object's name type, and displays name of object or Array
  let logUl = function(x) {
    if (isNaN(x)) {
      let output = document.createElement('UL');
      let outputText = document.createTextNode(x);
      output.appendChild(outputText);
      document.querySelector('body').appendChild(output);
    }
  }

  // Calls logLi on every value in object
  let isObject = function(x) {
    for (let i in x) {
      logLi(i, x[i]);
    }
  };

  //Updates Query url when user submits their city
  let city = function() {
    let api = "http://api.openweathermap.org/data/2.5/weather?q=";
    let units = "&units=metric&APPID=xxxxxxx"
    let url;
    url = api + cityName.value + units;

    //Breaks down the JSON
    $(document).ready(function() {
      $.getJSON(url, function(data) {
        let sort = function(x) {
          for (let k in x) {
            if (typeof x[k] == 'number' || typeof x[k] == 'string') {
              logLi(k, x[k]);
            } else if (Array.isArray(x[k])) {
              logUl(k);
              sort(x[k]);
            } else if (typeof x[k] == 'object') {
              logUl(k);
              isObject(x[k]);
            }
          }
        }
        sort(data);

      });
    });
  }
  //Submition
  button.onclick = city;
  cityName.addEventListener('keypress', function(e) {
    if (e.keyCode == 13) {
      city();
    }
  });

};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input id="city" value="London"></input>
  <button type="button" return false id="submit">submit</button>
</form>

Kenan Balija

Your code is a bit hard to read but i "cracked it". I made it work like this:

1) I've added a new div with an ID of "weather"

<form>
<input id="city" value="London"></input>
<button type="button" return false id="submit">submit</button>
</form>
<div id="weather"></div>

2) After this in js script add a new variable

let weather = document.getElementById('weather'); at begining inside window.onload function

3) In code change occurances of document.querySelector('body') with weather

4) In the end inside city function at begining add weather.innerHTML = ''; This will clear the html of weather like there is nothing in it.

Here is the example, just run the code snippet:

window.onload = function() {
  let cityName = document.querySelector("#city");
  let button = document.querySelector("#submit");
  let weather = document.getElementById('weather');
  // Checks value type and displays property and value.
  let logLi = function(k, j) {
    if (typeof j === 'object') {
      isObject(j);
    } else {
      let li = document.createElement('LI');
      let liText = document.createTextNode('\u00A0\u00A0\u00A0\u00A0' + k + ": " + j);
      li.appendChild(liText)
      weather.appendChild(li);
    }
  }
  // Checks Object's name type, and displays name of object or Array
  let logUl = function(x) {
    if (isNaN(x)) {
      let output = document.createElement('UL');
      let outputText = document.createTextNode(x);
      output.appendChild(outputText);
      weather.appendChild(output);
    }
  }

  // Calls logLi on every value in object
  let isObject = function(x) {
    for (let i in x) {
      logLi(i, x[i]);
    }
  };

  //Updates Query url when user submits their city
  let city = function() {
    weather.innerHTML = '';
    let api = "https://api.openweathermap.org/data/2.5/weather?q=";
    let units = "&units=metric&APPID=XXXXXXXXXXXXXX"
    let url;
    url = api + cityName.value + units;

    //Breaks down the JSON
    $(document).ready(function() {
      $.getJSON(url, function(data) {
        let sort = function(x) {
          for (let k in x) {
            if (typeof x[k] == 'number' || typeof x[k] == 'string') {
              logLi(k, x[k]);
            } else if (Array.isArray(x[k])) {
              logUl(k);
              sort(x[k]);
            } else if (typeof x[k] == 'object') {
              logUl(k);
              isObject(x[k]);
            }
          }
        }
        sort(data);

      });
    });
  }
  //Submition
  button.onclick = city;
  cityName.addEventListener('keypress', function(e) {
    if (e.keyCode == 13) {
      city();
    }
  });

};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input id="city" value="London"></input>
  <button type="button" return false id="submit">submit</button>
</form>
<div id="weather"></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i update the content in my database?

how do I update/delete a content in Django?

How do I get HTTP Request body content in Laravel?

How Do I Copy Body Content to Meta Title?

In Apigility, how do I access a DELETE request's body content

How do I make scrollbar only at div content and not at entire body?

How do I make content slide while erasing/adding it?

How to update content of an existing Azure blob with Python

How do I update content in PPT textbox while not changing the format

How do I update div when the text content changes in React

How do share full body content

<head> content is in <body> instead of <head> when I include it as header

How to update the value of an existing row instead of adding new row

How do I parse a JSON body in Rocket when the content type is not "application/json"?

How do I print the entire bootstrap modal where content in modal body is scrolled out of view

How do I remove content from a modal-body upon closing the modal?

How do I set the Content-Type of an existing S3 key with boto3?

How do I make modifications to existing layer(Optional Content Group) in pdf?

How do you check systemd update content?

How do I get Angular 2/4 to update existing DOM nodes instead of deleting+creating them?

xmlstarlet do not update content

How do I skip a button in tab order and in screen reader cursor when adding content?

How do i get my DIVS to change content just like the buttons, without adding an onclick function on the html

Postman showing 204 No Content instead of Response body

How do I call the string content of a variable containing a filepath instead of the file at the end of the filepath? Bash/Linux

Using the HttpClient how can I view the request content body?

How can I tell Swashbuckle that the body content is required?

How can I know if a json body has content?

How can I insert the content of a combobox into the body of a word document?