Ways to take user input in JS?

Tasked with building a library app from odin project, I've created my initial setup, next is to create an addBookToLibrary function that takes user input then saves the new book object as an array. I'm likely overthinking this as I did previous projects on the cli and used prompt-sync, for this there's probably a way of doing it with inputs text fields...?
6 Replies
Jochem
Jochem•17mo ago
there's prompt https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt As for input fields, you can either use a form and hook into the onsubmit event, or you can just have an onclick on a button, or even onchange on the field itself
Window: prompt() method - Web APIs | MDN
window.prompt() instructs the browser to display a dialog with an optional message prompting the user to input some text, and to wait until the user either submits the text or cancels the dialog.
CDL
CDLOP•17mo ago
That's true, I'll check it out thanks
Jochem
Jochem•17mo ago
I wouldn't use prompt too much btw, you can't style it at all it's also really annoying if you have multiple things to input
CDL
CDLOP•17mo ago
I was probably going to go along with the input idea just trying to figure out how I do it as it's been so damn long since I've used DOM and inputs xD
Jochem
Jochem•17mo ago
excuse the completely empty CSS field, but here's a quick demo of all three input based options: https://codepen.io/jochemm/pen/NWEzxGM you'd probably want to declare some more variables, I just chained the querySelector and addEventListener to keep it compact and keep things together for the demo
CDL
CDLOP•17mo ago
That's dope, thanks Jochem, I'll reference this and go from there 😛
Want results from more Discord servers?
Add your server