David Khakim
David Khakim

Reputation: 11

How to assign one variable to other inside of an object vanilla JavaScript

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();

Upvotes: 0

Views: 47

Answers (1)

skyline3000
skyline3000

Reputation: 7913

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();

Upvotes: 1

Related Questions