Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace first character of string in loop

On the page, the calendar columns are displayed with abbreviations, such as "sat" instead of "saturday".

<div class="dayClass">sat, 14 may</div>
<div class="dayClass">sun, 15 may</div>
<div class="dayClass">mon, 16 may</div>

to be like below

<div class="dayClass">Saturday, 14 may</div>
<div class="dayClass">Sunday, 15 may</div>
<div class="dayClass">Monday, 16 may</div>

I wrote this code by searching
But I can’t just change the word "day" and the date will also be replaced

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

window.onload = function findAndReplaceDayName(){
  var elements = document.querySelectorAll('.dayClass');
for ( var i=elements.length; i--; ) {
  var text = elements[i].innerText;
   if(text.slice(0,3) == 'sat' ){
    elements[i].textContent = "Saturday"
  }else if(text.slice(0,3) == 'sun'){
elements[i].textContent = "Sunday";
    }else if(text.slice(0,3) == 'mon'){
elements[i].textContent = "Monday";
}}

But the result is this

<div class="dayClass">Saturday</div>
<div class="dayClass">Sunday</div>
<div class="dayClass">Monday</div>

Can you help me to change only the day and not the date.
Thankful
Footnote: I am new to both programming and English language

>Solution :

window.onload = function findAndReplaceDayName() {
  var elements = document.querySelectorAll('.dayClass');
  for (var i = 0; i < elements.length; i++) {
    var text = elements[i].innerText;
    if (text.indexOf(',') !== -1) {
      var dayName = text.slice(0, text.indexOf(',')).trim();
      var date = text.slice(text.indexOf(',') + 1).trim();
      switch (dayName) {
        case 'sat':
          dayName = 'Saturday';
          break;
        case 'sun':
          dayName = 'Sunday';
          break;
        case 'mon':
          dayName = 'Monday';
          break;
        // add more cases for other days if needed
      }
      elements[i].textContent = dayName + ', ' + date;
    }
  }
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading