0

I created a variable with a string inside an object and need to split it inside of other variable in the same object.

function dateRestore(){

    var date = document.querySelectorAll('.dateEvent');

    //console.log(date_fin);
    for (var i = 0; i < date.length; i++) {

        var start = {
            date: document.querySelectorAll('.date_in')[i],
            dateSstring: this.date.innerHTML.split(' ')//here I try to assign the variable date to variable dateSstring 
        };
        var finish = {
            date: document.querySelectorAll('.date_fin')[i]
        }
        alert(start.dateSstring.innerHTML);

    }
}
dateRestore();
7
  • It's unclear what you are trying to accomplish. Can you elaborate on your expected result? Commented Jun 28, 2019 at 11:48
  • The string of 'date' variable is date in format 'day/month/year hour:minuts am/pm'. So I need to change the format. First I get the string to var date and after I want to split it in dateSstring. Other variable will be hourS: dateSstring[1], which is the time, after dayPeriod: dateSstring[2], which is the date and etc. If I do it inside of the object I can't assign one variable to another, appears an error "variable undefined". Commented Jun 28, 2019 at 11:56
  • With another words I need to do same like this: var start = document.querySelectorAll('.date_in')[i], var dateSstring = start.innerHTML.split(' '); var hourS = dateSstring[1]; var dayPeriod = dateSstring[2]; but inside of an object Commented Jun 28, 2019 at 11:58
  • There was a lot of strange bugs in your JS, check my editted version in snippet. Commented Jun 28, 2019 at 12:12
  • Here ? stackoverflow.com/review/suggested-edits/23392812 You should get a message where you can open edit and accept or reject it. Not sure if it is answer. Only edited your question. Commented Jun 28, 2019 at 12:21

1 Answer 1

1

Store the result of querySelectorAll() and then re-use it when creating the objects:

function dateRestore(){
    var date = document.querySelectorAll('.dateEvent');

    for (var i = 0; i < date.length; i++) {
        var date_in = document.querySelectorAll('.date_in')[i];

        if (!date_in) return; // make sure date_in exists

        var start = {
            date: date_in,
            dateSstring: date_in.innerHTML.split(' ')
        };
        var finish = {
            date: date_in
        };

        alert(start.dateSstring.innerHTML);
    }
}
dateRestore();
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.