0

Actually I'm working on a function that compares two dates (one date I get from my hidden input, another date - current datetime). I wrote a function in JS which works perfect:

var elements = document.getElementsByClassName("getTime");

        var names = [];
        for(var i=0; i<elements.length; i++) {
            names = elements[i].value;
            var dece = moment(names).format();
            var cureTime = moment().format();
            seco=moment().diff(dece, 'seconds');
            if ((Math.floor(seco / 60)) >= 1 && (Math.floor(seco / 60))<60) {
                console.log(Math.floor(seco / 60) + " minutes");
                document.getElementsByClassName("time").innerHTML = Math.floor(seco / 60) + " minutes ago";
            }else if(((Math.floor(seco / 3600)) >= 1) && ((Math.floor(seco / 3600))<24))
            {
                console.log(Math.floor(seco / 3600) + " hours");
                document.getElementsByClassName("time").innerHTML = Math.floor(seco / 3600) + " hours ago";
            }else if (((Math.floor(seco / 86400)) >= 1) && ((Math.floor(seco / 86400)) <30))
            {
                console.log(Math.floor(seco / 86400) + " days");
                document.getElementsByClassName("time").innerHTML = Math.floor(seco / 86400) + " days ago";
            }else if (((Math.floor(seco / 2592000)) > 1) && ((Math.floor(seco / 2592000)) <= 12))
            {
                console.log(Math.floor(seco / 2592000) + " months");
                document.getElementsByClassName("time").innerHTML = Math.floor(seco / 2592000) + " months ago";
            }else if((Math.floor(seco / 31536000)) > 1)
            {
                console.log(Math.floor(seco / 31536000) + " years");
                document.getElementsByClassName("time").innerHTML = Math.floor(seco / 31536000) + " years ago";
            }
        }

As a result all of those console.logs (in each if statement) return me the right difference in dates. But now I can't understand how can I parse all of these differences into the appropriate span fields with the same class name. My HTML looks like this:

@foreach($repoData as $kek)
     <span class="time" id="openedOn"></span>
    <input type="hidden" class="getTime" id="time" value='{!! $kek->created_at !!}'>
@endforeach

To be clear this FOREACH generates me several dates that are stored in the hidden input. Then I get values from this hidden input and pass them to my function in JS. So I can't understand how should I parse elements from my JS function to that span with class name - "time". Please help.

7
  • 2
    getElementsByClassName returns an HTML Collection, not a single element.... Commented Jun 19, 2017 at 15:03
  • stackoverflow.com/questions/10693845/… Commented Jun 19, 2017 at 15:04
  • 2
    its getElements(plural), it returns an array. You need to do document.getElementsByClassName("time")[0].innerHTML Commented Jun 19, 2017 at 15:05
  • Wow. Thank you @NikhilNanjappa, I rewrote my code with a document.getElementsByClassName("time")[i].innerHTML and it works now. Good job! Commented Jun 19, 2017 at 15:17
  • 1
    Don't use [0] inside the loop, you can simply make use of the counter(i). Commented Jun 19, 2017 at 15:21

1 Answer 1

1

getElementsByClassName functions returns an array(Read more here) and hence you need to use array notation to access its elements

document.getElementsByClassName("time")[0].innerHTML

In your case, since your accessing the elements within a loop you should simply make use of the counter(i).

document.getElementsByClassName("time")[i].innerHTML

Replace the above to all the conditions for it to work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.