jeudi 17 janvier 2019

how can I store key and value to array storage?

I had stored my input on a object(key and value) using:

const object1 = {
    un: inp.value,
    pw: inpw.value
};
var myJSON = JSON.stringify(object1);
var myObj = JSON.parse(myJSON);

now I want every object to be stored in an array. for example the first

input are:

  • {"un":"john","pw":"smith"}

it will be stored to array[0] the second input then will be for example:

  • {"un":"beth","pw":"sebastian"}

and so on..

so when I call array[0], it will only display {"un":"john","pw":"smith"}'

so this is my code:

<form action="" autocomplete="on">
  <div class="" style="width:300px;">
    <input id="myInput" type="text" name="myInput" placeholder="Input" autocomplete="input">
    <input id="myPW" type="password" name="myPassword" placeholder="Password" autocomplete="password">
  </div>
  <input id="button" type="submit">
</form>

    <h2>Username</h2>
    <p id="uname"></p>

    <h2>Password</h2>
    <p id="pass"></p>

    <h2>Data</h2>
    <ol id="val"></ol>

    <h2>Array</h2>
    <ol id="arr"></ol>

<script>
var myButton = document.getElementById('button');
var inp = document.getElementById('myInput');
var inpw = document.getElementById('myPW');

//var inputData = [];

myButton.addEventListener('click', function(event) {
    event.preventDefault();

    const object1 = {
        un: inp.value,
        pw: inpw.value
    };
    var myJSON = JSON.stringify(object1);
    var myObj = JSON.parse(myJSON);

    val.innerHTML += '<li>' + myJSON + '</li>';

    cookies[i];
    cookies.push(myJSON);
    for (var i = 0; i < cookies.length; i++) {
        arr.innerHTML += '<li>' + cookies[i] + '</li>';
    }       
});

PLEASE HELP ME TO FIX IT




Aucun commentaire:

Enregistrer un commentaire