0

I'm trying to get an array of strings from an input field and clear the field every time keyup(enter) (It's an exercise, i'll snip the full challenge in: 1. Single Message

Create a message input where you can type any message. When you press Enter, the message should appear inside a "messages" box and the input should clear

  1. Multiple Messages

When creating a new message, instead of replacing the message inside the "messages" box, add the new message at the bottom of the last one).

I'm getting main.js:9 Uncaught TypeError: Cannot read property 'textContent' of null, i've tried value and get the same thing, i've tried just by targeting the id and get undefined or null.

I'm new to Jquery, i've read through the documentation, but i've tried a number of different methods and nothing seems to be working.

here is my HTML

    <body>
        <div class="container-fluid">
            <div class="container">
            <form id="msg-input">
                <input type="text" placeholder="sample text" id="input-test" action="submit">
            </form> 
            </div>
            <div class="alert alert-info" id="alert-primary" role="alert">
                msg goes here
            </div>

Here is my JQuery, i've tried everything I could think of including - ading .value() .textcontent(), etc I just need the input to be saved into var array on submit of input box and box reset

$('#alert-primary').hide()// hides first msg initially

$('#msg-input').on('submit', function(e) { //on submit prevents refresh
    e.preventDefault(); 

$('#msg-input').each(function() { //loops through each ipnut 
    var messages = []; // initializes var messages
        messages.push((document.getElementById("#input-test").textContent()));// pushes string to messages array
$('#msg-input').reset();     //resets input field
    });
 console.log(messages); //logs messages
})

I'd kind of like a message text box (intercom, fb etc.. ). but for now like to be able to store the inputs. i'll generate the messages later. the objective here is to use javascript.

Taplar
  • 24,788
  • 4
  • 22
  • 35

1 Answers1

0

Try something like this:

var messages = []; // Take care to not clean your array in "each" execution

$('#msg-input input').each(function() { // Get each input inside forms
    messages.push(this.value); // Get value of this (input)
});