H
H
holfizz2022-04-21 13:47:44
JavaScript
holfizz, 2022-04-21 13:47:44

How to properly remove an element from an array?

it is necessary that the last letter from the array is removed on 'c'.
I did everything as on the Internet, then 2 elements then appear. In general, I did not really find anything. Help I'm just learning

let a = document.querySelector('.a'),
    b =  document.querySelector('.b'),
    c =  document.querySelector('.c');

let array = [a,b]

let pup = []
for (let element of array) {
    element.addEventListener('click', ()=>{
        pup.push(element.value)
        let input = document.querySelector('.input')

        input.value = pup.join('')
        c.addEventListener('click', ()=>{
            
        })
    })
}


tried filter shift pop splice apparently did something wrong))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WapSter, 2022-04-21
@holfizz

const a = document.querySelector('.a'),
    b =  document.querySelector('.b'),
    c =  document.querySelector('.c');
const input = document.querySelector('.input')
const array = [a,b,c]

let pup = []
for (let element of array) {
    element.addEventListener('click', ({target})=>{
        if(['a','b'].includes(target.value)) {
          pup.push(target.value)
        } else {
          pup.pop()
        }
        input.value = pup.join('')
    })
}

N
Ne7Le4Der, 2022-04-21
@Ne7Le4Der

https://codesandbox.io/s/awesome-pond-e176yh
I don't know if you have this in mind, but you hang up the handler on "c" only after clicking on one of the elements, this also needs to be taken into account.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question