Show the current temperature data retrieved from the weather API

I'm trying to figure out what's causing the issue with the temperature not displaying. Can anyone help me identify and fix the error?

<script type="text/javascript">
  $.ajax({
    url: 'https://api.weather.gov/gridpoints/EWX/155,90/forecast/hourly'
  }).done(function(res) {
    var temp = res.properties.periods[0].temperature;
  });
  document.getelementbyid('temperature').innerHTML = temp
</script>
<p>
  The current temperature should be displayed as <span id="temperature"></span>F.
</p>

Answer №1

There are a couple of issues at play here. First and foremost, there's a problem with scoping when attempting to access "temp" outside the scope where it's defined. It's only set within the .done() function.

The second issue arises from using document.getelementbyid instead of document.getElementById(), as outlined in the official specification here.

<script type="text/javascript">
$(function() {
  $.ajax({
    url: 'https://api.weather.gov/gridpoints/EWX/155,90/forecast/hourly'
  }).done(function(res) {
      document.getElementById('temperature').innerHTML = res.properties.periods[0].temperature;
  });
});
</script>
<p>
    The current temperature is <span id="temperature"></span>F.
</p>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Design blueprint for mysterious JSON structure

I'm seeking a little guidance on how to create a class model for a specific JSON tree that is generated by a webservice. Unfortunately, I have no control over the structure of the JSON. The JSON data looks like this: { "version":"1", "value ...

What is the best way to retrieve table field values and display them in a modal box?

Data Table Display <tbody id="batch_view" class = "a-checkbox"> <% @batches.each do |batch| %> <tr> <td class="<%= batch.id %>"><%= batch.date.strftime("%Y-%m-%d") if !batch.date.nil? %></td> <td> ...

Angular setPristine function is not functioning properly

I am looking to achieve a simple task - cleaning the $scope.user fields without encountering errors. if ($scope.contactForm.$valid) { $scope.user = {}; $scope.contactForm.$setPristine(); } However, I'm still experiencing v ...

Delete the item along with its associated data from the local storage upon clicking on an icon

I'm currently working on a To-Do List and facing an issue while trying to delete an item upon clicking a trash bin icon. image The problem I'm encountering is that only one data point is removed from local storage when the icon is clicked. If I ...

Django - tallying timer in action

Looking to add a timer that begins when accessing this template: {% block content %} <h3>C-Test</h3> <p>In the text below, some word endings are missing. The gap is about half the length of the word, so you need to fill in the rest ...

There was an issue retrieving the value from the $.ajax() error function, as it returned [

After successfully receiving data from the input field and sending it to the database, everything seems to be working fine. However, when attempting to retrieve the data after sending it to the database, an error is encountered: [object HTMLInputElement]. ...

Trouble uploading an audio blob as a base64 string using Google Drive API with the drive.files.create method - File ID could not be located

My current challenge involves sending an audio blob to a specific Google Drive folder. To accomplish this, I convert the blob into a file before initiating the transfer process. However, I have encountered an error from the beginning: Error: File not fo ...

Using a combination of Javascript and PHP to dynamically update the innerHTML of multiple elements

I am working on updating the innerHTML of a webpage using Javascript after an onclick event. In my java.js file, I am utilizing this code to access a PHP page that echoes back the text for updating the innerHTML. The issue arises when attempting to update ...

What could be the reason for the RecyclerView not displaying any content in this particular fragment?

Setting up a simple template app with a ViewPager to experiment and get acquainted with Fragments and ViewPagers was a fun learning experience. The TabbedActivity class in my app contains a fragment class called LinearFragment: public static class LinearF ...

Popover disappears when scrolling the page, but its position remains constant in Angular 4+

After clicking on an icon, a popover appears. I've noticed that the popover has a delay set, but when I scroll, the popover also moves along with it. Is there a way to keep the popover in place and not have it affected by scrolling? <md-badge cla ...

The positioning of the menu icons varies

When it comes to displaying the search icon in the WordPress toggle bar, I use a custom JavaScript file. This setup is mainly focused on website design and menu functionality. Additionally, I have integrated a newsletter subscription plugin for managing su ...

Develop a JSON structure by retrieving nested documents using Firebase Firestore and Express

I'm currently working on developing an application using Express and Firebase Cloud Functions. I'm facing a challenge in creating a nested JSON structure based on the database schema specified below: Below is the code snippet that I am using: ex ...

Sending user input from search component to main App.js in React

I'm currently working on an app that searches a Movies database API. I have a main fetch function in App.js, and in tutorials, people are using a search bar within this main APP component. I'm wondering if it would be better to create a separate ...

Trigger Alert Prior to Reloading the Page

jQuery confirm min js is being used, along with alert in the ajax code. An issue arises where locationReload and reload are given priority before alert runs. The desired sequence is to have alert run first, followed by the reload. An observation made is ...

Creating a seamless integration of images and backgrounds using CSS

Can you help me find a way to blend the image with the background to eliminate the visible separation between them? Here is the link to the image: https://i.stack.imgur.com/VB9in.jpg ...

Submitting an Ajax form refreshes the page

After submitting the form, the page reloads and I am trying to prevent that from happening. Latest Update: After receiving some feedback, I have made changes to my code. The form submission now works correctly, but the page still reloads. Previously, this ...

What is the best location to specify Access-Control-Allow-Origin or Origin in JavaScript?

After researching, I found out that I need to set my Access-Control-Allow-Origin or Origin tags. But the question is: where do I actually add these? The suggestions were to use them in the header section. I couldn't find any detailed explanation on t ...

The identical page content is displayed on each and every URL

Implementing a multi-step form in Next JS involves adding specific code within the app.js file. Here is an example of how it can be done: import React from "react"; import ReactDOM from "react-dom"; // Other necessary imports... // Add ...

Looking to enable input customization in a rendered ReactJS component? Should I elevate the state?

On my webpage, I successfully displayed the values Hello and World, but currently, they cannot be edited. Even though the text cursor shows up when clicked on, keyboard input doesn't register. How can I enable input functionality like deleting or addi ...

The javascript function in my file isn't being triggered by jquery

In my ASP.NET MVC project, I am facing an issue with a jQuery function in a JavaScript file located under the Scripts folder. This function is supposed to fill a textbox with the selected value upon clicking a dropdown, but for some reason it is not workin ...