How to rewrite such code snippet to pure JS?

$('.temp').each(function(i){ var tempMin = Math.floor(weather.forecast.forecastday[i].day.mintemp_c); var tempMax = Math.floor(weather.forecast.forecastday[i].day.maxtemp_c); $(this).append("<p>" + tempMin + "/" + tempMax + "&degC"); }); 
  • one
    This question should be closed, because it is useless for the knowledge base - aleksandr barakin

3 answers 3

 Array.from(document.getElementsByClassName("temp")).forEach(function(e, i) { var tempMin = Math.floor(weather.forecast.forecastday[i].day.mintemp_c); var tempMax = Math.floor(weather.forecast.forecastday[i].day.maxtemp_c); e.innerHTML = "<p>" + tempMin + "/" + tempMax + "&degC</p>"; }); 
  • one
    after all, e.innerHTML = ... has the side effect of deleting all existing content - Igor
 var temps = document.querySelectorAll('.temp'); for (var i = 0; i < temps.length; i++) { var tempMin = Math.floor(weather.forecast.forecastday[i].day.mintemp_c); var tempMax = Math.floor(weather.forecast.forecastday[i].day.maxtemp_c); var p = document.createElement("p"); p.innerHTML = tempMin + "/" + tempMax + "&degC"; temps[i].appendChild(p); } 

    Something like this:

     var els = document.querySelectorAll('.temp'); for (var q=0; q<els.length; ++q) { var tempMin = Math.floor(weather.forecast.forecastday[q].day.mintemp_c); var tempMax = Math.floor(weather.forecast.forecastday[q].day.maxtemp_c); els[q].insertAdjacentHTML('beforeend', "<p>" + tempMin + "/" + tempMax + "&degC"); }