DEV Community

loading...

Discussion on: How do I get the first/last char of a string from a user input (input.value)

Collapse
donnii profile image
Daniel Dark • Edited

There's a typo

wrong:

var yournmae = document.querySelector("#yn");

correct:

var yourName = document.querySelector("#yn");
Collapse
morrisjohn profile image
Morris John Author

Okay thanks. I've corrected it

Collapse
donnii profile image
Daniel Dark

And here is what you want:

submit.addEventListener('click', () => {

    const {value} = yourName

    const firstChar = value.slice(0, 1)
    const lastChar = value.slice(-1)

    div.classList.add('hide')
    welcome.textContent = firstChar + lastChar
})
Thread Thread
morrisjohn profile image
Morris John Author • Edited

Wow, that worked.

I don't even understand the code (*LOL I haven't learnt the 'const / {} => yet *) but yeah it worked.

Thank you very much