Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find value of 3 inputs

I’ve made a form that pops up and now I want to store the input values in 3 separate values in the array const book.

The project will be a library app where you’ll ultimately be able to remove/add books, and it’s apart of The Odin Project’s curriculum.

Here is my repository: https://github.com/Rutchyy/library (focus around line 53 in the app.js file.)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const book = submit.addEventListener("click", (event) => {
    event.preventDefault();
    let name = newForm.firstChild.firstChild // How do I navigate to the title input?
    console.log(name)
})

And the HTML:

<form method="get" id="new-book-form">
            <div>
                <label for="title">Title</label>
                <input type="text" id="title" name="title">
            </div>
            <div>
                <label for="author">Author</label>
                <input type="text" id="author" name="author">
            </div>
            <div>
                <label for="pages">Pages</label>
                <input type="number" id="pages" name="pages">
            </div>
            <button id="create-book">Create Book</button>
        </form>

>Solution :

submit.addEventListener("click", (event) => {
    event.preventDefault();
// book variable be edited afterwards
    let book = []
//select all inputs of the form
    document.querySelectorAll('#new-book-form input').forEach(e =>{
        book.push(e.value)
    })
    console.log(book)
})

At first I created an empty array, then fetched all inputs of the form by querySelectorAll() which selects all elements matching css selector #new-book-form input, then for each of them, I inserted their value to the array.

Hope it helps ^_^

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading