Is it possible for Skycons to show a duplicate icon?

My current issue involves integrating the JavaScript plugin "Skycons" with the Yahoo weather RSS feed. The problem arises when multiple days have the same weather forecast, as the plugin retrieves icons based on ID rather than class. This prevents me from displaying the same icon more than once.

For instance, all icons below will display except for the last li element due to the repeated use of the ID "snow":

<ul class="days">
       <li class="col-md-3 col-sm-3 col-xs-3"><strong>Saturday</strong>
             <canvas id="snow" width="50" height="50"></canvas>
             <span>19&deg;</span>
       </li>
       <li class="col-md-3 col-sm-3 col-xs-3"><strong>Sunday</strong>
             <canvas id="rain" width="50" height="50"></canvas>
             <span>19&deg;</span>
       </li>
       <li class="col-md-3 col-sm-3 col-xs-3"><strong>Monday</strong>
             <canvas id="sleet" width="50" height="50"></canvas>
             <span>19&deg;</span>
       </li>
       <li class="col-md-3 col-sm-3 col-xs-3"><strong>Wednesday</strong>
             <canvas id="snow" width="50" height="50"></canvas>
             <span>19&deg;</span>
       </li>
</ul>

Below is my initialization JS:

<!--SkyCons-->  
<script type="text/javascript" src="js/vendors/skycons/skycons.js"></script> 
<script>
      var icons = new Skycons({"color": "#fff"}),
          list  = [
            "clear-day", "clear-night", "partly-cloudy-day",
            "partly-cloudy-night", "cloudy", "rain", "sleet", "snow", "wind",
            "fog"
          ],
          i;

      for(i = list.length; i--; )
        icons.set(list[i], list[i]);

      icons.play();
    </script> 

Additionally, you can access the referenced JS file at the following link:

https://github.com/darkskyapp/skycons/blob/master/skycons.js

Answer №1

It is recommended to utilize the version that operates with element references instead of relying on id

(Additionally, modify the HTML structure to employ classes rather than ids)

for(i = list.length; i--; ) {
    var weatherType = list[i],
        elements = document.getElementsByClassName( weatherType );
    for (e = elements.length; e--;){
        icons.set( elements[e], weatherType );
    }
}

and update your HTML to

<li class="col-md-3 col-sm-3 col-xs-3"><strong>Saturday</strong>
         <canvas class="snow" width="50" height="50"></canvas>
         <span>19&deg;</span>
   </li>
   <li class="col-md-3 col-sm-3 col-xs-3"><strong>Sunday</strong>
         <canvas class="rain" width="50" height="50"></canvas>
         <span>19&deg;</span>
   </li>
   <li class="col-md-3 col-sm-3 col-xs-3"><strong>Monday</strong>
         <canvas class="sleet" width="50" height="50"></canvas>
         <span>19&deg;</span>
   </li>
   <li class="col-md-3 col-sm-3 col-xs-3"><strong>Wednesday</strong>
         <canvas class="snow" width="50" height="50"></canvas>
         <span>19&deg;</span>
   </li>

If compatibility with IE8 and earlier versions is essential, incorporating this polyfill for the getElementsByClassName function is necessary: Polyfill for getElementsByClassName for particular uses

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

Is there a way to execute two files concurrently in JavaScript using node.js?

I'm a beginner in the world of Javascript and Node.js, and I've encountered some issues while trying to test code I recently wrote. Specifically, I am attempting to test the code within a file named "compareCrowe.js" using another file named "tes ...

A Typescript Function for Generating Scalable and Unique Identifiers

How can a unique ID be generated to reduce the likelihood of overlap? for(let i = 0; i < <Arbitrary Limit>; i++) generateID(); There are several existing solutions, but they all seem like indirect ways to address this issue. Potential Solu ...

``There Seems to be an Issue with Loading Content in Tabs

Hey! I'm encountering an issue with my jquery tabs. The first tab content loads fine, but when I select another tab, the content doesn't display. Then, when I try to go back to the first tab, it doesn't load either. Here's the HTML cod ...

The ng-repeat ng-switch combination is not functioning as expected

My data can be simplified to the following in my code: function DemoCtrl($scope) { $scope.myData= [ {text: "blah", other: 3, V: 'caseOne'}, {text: "blah", other: 3, V: 'caseTwo'}, {text: "blah", other: 3, V ...

Switching visual content according to dropdown choice

Hey there! I've been working on my HTML file and ran into a little issue. I'm trying to change the image in the div section based on a dropdown selection that modifies the source of the image from an XML file. However, I keep getting an error tha ...

issue with arranging content in a two-column layout with headers

Currently working on my first website and running into some CSS issues. I'm aiming for a two-column + header layout that fills the entire screen space of the website. I have the following specifications in mind: Header takes up 20% of the screen he ...

The xslt code is failing to invoke the JavaScript function

I am currently utilizing xslt for the transformation of xml to html. Below is an example of an .xml file. <ImportOrganizationUtility-logging> <log-session module-name="ImportOrganizationUtility" end="17:54:06" start="17 ...

Reduce code length for generating DOM fragment with jQuery

I'm currently working on generating a tree of elements that will be used as an input for JsTestDriver unit tests. I've got some code to create this tree which involves using Document Object Model methods in JavaScript. I'm wondering if there ...

What could be causing the slowdown in my NodeJS IRC-bot?

Decided to challenge myself today by creating an IRC-bot using JavaScript with NodeJS. So far, everything is functioning correctly, but it seems like there may be a bottleneck somewhere. For instance, when I input a command multiple times, the initial res ...

What is the best way to emphasize case-insensitive searchtext matches in JavaScript?

If I have data containing words like Krishna, krishna, KRISHNA and I enter the search text as 'krish', it will retrieve all three words. However, when I want to highlight the matching result, only the exact matching part of the string is highligh ...

How to Enhance Angular ui-router nested views with Resolve?

I have been working on creating a customized version of my Angular 1.4.12 application with nested views. The main purpose behind this customization is to accommodate sections within the app that require a header/content/footer structure, while others do no ...

In PowerShell, use the "ConvertTo-Html" commandlet to incorporate

I have a script that retrieves data from a server, converts it to HTML, and then sends the report via email. Here is a snippet of the script: $sourceFile = "log.log" $targetFile = "log.html" $file = Get-Content $sourceFile $fileLine = @() foreach ($Line i ...

Adjusting the size of an object as the page dimensions change

I am looking to dynamically resize an element whenever the document resizes. For example, if a draggable div is moved and causes the document to scroll, I want to adjust the size of another element using $("#page").css('height','120%'); ...

Angular 8 utilizes JavaScript for its programming language

Looking for a solution in JavaScript - check out: codepen.io/skovtun/pen/VwLvXPB Struggling to find an equivalent for Angular 8+. I want the center block to maintain a fixed width of 1200px, and automatically compress when the left, right, or both sideb ...

Swapping out LinkButtons using jQuery

[EXPLANATION] (apologies for the length) In my gridview, users have the option to add a new entry or edit an existing one. When they click on either 'Add' or 'Edit', a popup window appears for them to input the information for the new ...

React Material-UI Multiple Select with Checkboxes not displaying initial selections as checked

I am fairly new to working with React and MUI, so please bear with me. Currently, I am in the process of learning how to create Multiple Select Options with checkboxes by populating the Dropdown Options from an Array. Although I have set up the initial/de ...

Real-time database logging triggered by Firebase user authentication

exports.setupDefaultPullups = functions.auth.user() .onCreate( async (user) => { const dbRef= functions.database.ref; let vl= await (dbRef.once('value').then( (snapshot) => { return snapsh ...

Display header right button based on a condition in React Native using React Navigation

I am looking to conditionally display the Entypo new-message icon in the top right corner of the header based on a boolean variable (if true, then show the icon in the header). Here is a snippet of code where this functionality can be replicated. Thank y ...

"Experience the power of utilizing TypeScript with the seamless compatibility provided by the

I'm attempting to utilize jsymal.safeDump(somedata). So far, I've executed npm install --save-dev @types/js-yaml I've also configured my tsconfig file as: { "compilerOptions": { "types": [ "cypress" ...

What is the best way to incorporate raw HTML code into a .jsx file within a NextJS website?

I need to integrate Razorpay code into my NextJS website, but it's currently in pure HTML format. For example, the code looks like this: <form><script src="https://cdn.razorpay.com/static/widget/subscription-button.js" data-subscrip ...