0

Here is the code, below the console.log part is my issue.

It logs out all 3 classes of offset in a tag like this :

<tag id="id1" class="$offset1, $offset2, $offset3">

But I just need 1 class of offset in a tag like this:

<tag id="id1" class="$offset1">

How do I fix this issue?

Explanation:

add new classes to html element

function $addClsls(element, ...newClass) {
    return element.classList.add(...newClass);
}

remove classes from html element

function $rmClsls(element, ...rmClass) {
    return element.classList.remove(...rmClass);
}

select an element id or class

function $select(element) {
    return document.querySelector(element);
}

a function to add or remove class from a selected element

function $switch(selectEle, eleClass, indexNum) {
    switch (indexNum) {
        case 0:
            $addClsls(selectEle, eleClass);
            break;
        case 1:
            $rmClsls(selectEle, eleClass);
            break;
        default:
    }
}

get 3 element ids into array

let groupElements = [$select('#id-1'), $select('#id-2'), $select('#id-
3')];

get 3 element classes into array(no need to put . )

let groupOffsets = ['offset1', 'offset2', 'offset3'];

set init time value

let addTime = 0;

check if true to map through groupElements and groupOffsets and set delay time

if (true) {
    groupElements.map( i => {
      setTimeout( () => {
        $switch(i, groupOffsets.map( x => x ), 0)
        console.log(i) // <= logs out results
      }, 0 + addTime );
      addTime += 1000;
    });
}

the GOAL is to log out like this :

<tag id="id-1" class="offset1"> </tag>
<tag id="id-2" class="offset2"> </tag>
<tag id="id-3" class="offset3"> </tag>

but the issue is like this :

<tag id="id-1" class="offset1,offset2,offset3"></tag>
<tag id="id-2" class="offset1,offset2,offset3"></tag>
<tag id="id-3" class="offset1,offset2,offset3"></tag>
3
  • Please provide a runnable minimal reproducible example along with a better explanation of how to determine which class to use Commented Jan 8, 2018 at 14:37
  • groupOffests[i] instead of groupOffsets.map(x =>x) ? Commented Jan 8, 2018 at 14:54
  • i have tried groupOffsets[i], it logs out undefined :( Commented Jan 8, 2018 at 15:24

1 Answer 1

1

Well, I got it fixed. So I'm gonna answer my own question. ;)

I tried another way to do map function, I used a 2-dimensional array instead of 2 separated arrays

let groupItems = [ 
    [$select('#id-1'), 'offset1'],
    [$select('#id-2'), 'offset2'],
    [$select('#id-3'), 'offset3']
]
groupitems.map( items => {
            setTimeout( () => {
                // destruct first dimensional
                let [ x ] = [items];
                // destruct second dimensional
                let [ i, j ] = x;
                // first round i = $select('#id-1'); j = 'offset2';
                // second and third round so forth
                sectionSwitch( i, j, 1);
            }, 0 + addTime);
            addTime += 1000;
        })
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.