Change in color due to the change in temperature of the

I recently completed a jQuery weather project using data from Wunderground. I am trying to implement a feature where the color of the temperature changes automatically based on whether it is higher or lower, but I am unsure how to incorporate CSS within jQuery.

function check(temp_c) {
      // Code for checking temperature range and returning corresponding value
    }


var temp_c = data.current_observation.temp_c ;
var temperature = check(temp_c );

Answer №1

To streamline your code, you can simplify it by focusing on determining the upper temperature limit and allowing the program to flow through each condition to identify the highest value. Consider the following approach:

function assessTemperature(temp) {
    temp = parseFloat(temp);
    var tempCategory = 'above326'; // default category for temperatures above 326        
    if (temp < 326.25) tempCategory = 'below326'; 
    if (temp < 303.75) tempCategory = 'below303';
    if (temp < 281.25) tempCategory = 'below281';
    // continue with additional categories...

    return tempCategory;
}

You can then apply the returned category using the addClass() method:

var currentTemp = data.current_observation.temp_c ;
var determinedCategory = assessTemperature(currentTemp);
$('#myElement').addClass(determinedCategory);

See live demonstration here

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

Reduce the amount of ajax calls

Currently, I have implemented checkboxes that serve as search filters on a website. Every time a user checks a filter box, an ajax request is triggered to fetch data from the server and display it. The issue arises when users select multiple checkboxes in ...

Arranging form fields for uploading files in sails.js

I am facing an issue where I cannot receive parameters after the file upload parameter. This problem is mentioned in Skipper's documentation. The suggestion is to reorder the parameters before submitting the form and sending them to the server. I have ...

After the form is submitted on the current page, it will not submit again using jquery unless I refresh the page

Currently, I am attempting to submit my form on the same page utilizing jquery. However, after the initial submission, any further attempts fail. It seems that in order for the submission to work properly again, I need to refresh the page first. What cou ...

CSS - revealing additional submenu on hover

I'm currently working on customizing a Wordpress menu that includes classic menu items and their respective submenus. The issue I'm facing is that when I hover over an item with children, its submenu will display. However, I want to have a fixed ...

Can I send JavaScript variables to a different .PHP file by using Ajax?

I need to transfer javascript variables from one file where calculations are performed to another file containing a mySQL query. The second file is loaded using a .load() function without refreshing or redirecting to it. Is it possible to achieve this wit ...

Determine whether certain radio buttons are selected using jQuery

JavaScript $('input').change(function() { $('input:radio').prop('disabled', true); $('.answer-detail').show(); $(this).next('label').addClass('correct'); var correctAnswers = ("#answer ...

Choose the checkbox if the tablerow includes specific text

I am working with a table that has two columns. The first column consists of checkboxes and the second column contains attribute names. My goal is to select the checkboxes next to a cell that includes specific text. Below is the structure of my table: &l ...

Switch up image origin when image reaches screen boundary

I am trying to create a DVD screensaver with changing images each time it hits the edge, but I can't seem to get it to work. I've attempted using the code snippet below: var img = document.getElementById("myImage"); img.src = 'img/new-image. ...

Remove all $.ajax requests from content that has been loaded using $.ajax in jQuery

I'm currently working on a page where users can click a link to load content using $.ajax into a designated container div. However, I've encountered an issue with multiple clicks causing an increase in the number of $.ajax requests and resulting ...

Utilize the map function to showcase API data on Next.js pages

I am encountering an issue while trying to display data on the page. The error message "data is not defined" keeps appearing. I would appreciate it if you could take a look at the code and help me resolve this problem. import { useEffect } from "react ...

Tips on how to customize an HTML form submission appearance

Take a look at my file upload form below: <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="filetoupload" id="filetoupload" onchange="this.form.submit();"> </form> I am trying to figure out ...

Developing SAPUI5: Construct a GenericTile featuring a centrally positioned icon

I'm having trouble creating a custom tile in SAP that inherits from sap.suite.ui.commons.GenericTile and only displays an icon centered in the middle of the tile. The standard aggregations and properties are causing issues as they position the icon on ...

When you scroll a fixed element on the page, the block is truncated

I made a modification for adding cart items and included a script for managing my cart The cart is supposed to stick to the right edge and scroll up and down on the page Everything seems to work, but there's a slight issue when I click on Add 1 and ...

Tips for changing the CSS pseudo class of an HTML element with JavaScript?

Here's a code snippet I have: const myDiv = document.querySelector("#myDiv"); myDiv.addEventListener("click", fct_Div); function fct_Div(ev) { console.log(ev); if (ev.altKey === true) { myDiv.style["background ...

Changing the color of a div's text based on its content with CSS

I am facing a challenge in styling the text inside a “div” element using a Javascript function. Initially, when the page loads, these “divs” contain no value. The values of the "divs" will change between "Open" and "Closed" twice a day when the Jav ...

Tips for utilizing a template while inserting a new row into a Kendo UI grid

When attempting to add a new row to a kendo grid, I encountered an issue with inserting a dropdown template in one of the cells. The current method I am using to add a new row is as follows: grid.dataSource.insert(0, { AreaID: null, AreaName: "New Area", ...

Create an array, analyze it, and retrieve a specific value

My goal is to achieve the following: var price = $.cookie('cur_price'); //this will be USD, EUR, or GBP var price = 'USD'; //this is a possible result var USD = 30; //store in an array var EUR = 24; //this seem ...

Arrange JSON response data in alphabetical order

Looking to sharpen my skills by working with public APIs in JavaScript. My goal is to sort the list of items alphabetically by the h4 value (food name) when the query is submitted. I attempted to use .sort() on the response item before using .map(), but en ...

Enhance your webpage with a dynamic form feature for adding multiple products using jQuery

I'm currently working on a project to develop a dynamic form that includes both input fields and dropdown menus populated with data from a JSON file (for now, I'm using an array). The form should also have the functionality to "add another produc ...

What is the best way to access JSON stringified objects in PHP?

I recently used the following code snippet to send data to the server, but now I'm stuck on how to retrieve the array that was returned using PHP. Any suggestions would be greatly appreciated. $('.ticket-row').each(function() { tickets.push ...