Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
span textContent split returns undefined in javascript funtion

I am trying to help a friend out with the following javascript. We both are new to javascript.

We are trying to convert the date string to a different format. Based on internet search we understand that the date function expects the input string to be Date(year, month, day) format. To achieve that we need to parse the input string and send it in the expected format. We have not got a clue why string split on span tag's textcontent (that contains the date string) is not working.

<script type="text/javascript">
function ready(callback){

    if (document.readyState!='loading') callback();
    else if (document.addEventListener) document.addEventListener('DOMContentLoaded', callback);
    else document.attachEvent('onreadystatechange', function(){
        if (document.readyState=='complete') callback();
    });
}
    window.onload = ready(function() {
        var dateString = document.getElementById("dateFormatter").textContent.trim();
        var sMonth = dateString.split("/")[0];
        var sDay = dateString.split("/")[1];
        var sYear = dateString.split("/")[2];
        document.getElementById("dateFormatter").textContent=sMonth;
    });
</script>

The html has the following span tag.

<span id="dateFormatter">26/06/1993</span>

sMonth returns 26/06/1993, whereas sDay and sYear returns undefined.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The string from your div is invalid format if you want to convert to a datestring. A valid string would be: yyyy-mm-dd. If you have already a dateobject you can use the function to format the date.

Like that:

Update

callback();
window.addEventListener('load', function () {           
      var dateString = document.getElementById("dateFormatter").textContent.trim();
      console.log(dateString)
      let dateArr = dateString.split("/");
      var sDay = dateArr[0];
      var sMonth = dateArr[1];
      var sYear = dateArr[2];
      let newDateString = (sYear + '-' +  sMonth + '-' + sDay);
      let d = new Date(newDateString);
      console.log(d);
      document.getElementById("dateFormatter").textContent=d.toLocaleString();
});
<span id="dateFormatter">26/06/1993</span>

old snippet

function ready(callback) {

    if (document.readyState!='loading') callback();
    else if (document.addEventListener) document.addEventListener('DOMContentLoaded', callback);
    else document.attachEvent('onreadystatechange', function(){
        if (document.readyState=='complete') callback();
    });
}

    window.onload = ready(function() {
      var dateString = document.getElementById("dateFormatter").textContent.trim();
      console.log(dateString)
      let dateArr = dateString.split("/");
      var sDay = dateArr[0];
      var sMonth = dateArr[1];
      var sYear = dateArr[2];
      let newDateString = (sYear + '-' +  sMonth + '-' + sDay);
      let d = new Date(newDateString);
      console.log(d);
      document.getElementById("dateFormatter").textContent=d;
    });
<span id="dateFormatter">26/06/1993</span>

about 4 years ago · Juan Pablo Isaza Report

0

The problem is that you're setting the textContent of the span to only show the value of sMonth and when that happens, the span will only have "26" as its body (whitch btw is wrong if you write dates like so) - and actually should not be "26/06/1993" if the code is right.

Also, if you want to run the script multiple times, try setting the span's textContent to sDay+"/"+sMonth+"/"+sYear so it's the same formatting you expect the script to work with. The code you provided sets the span's value to "26" then if you run it again, it tries to split "26" along "/"-s, which of course ends up being incorrect.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!