html - Get value from input and store it to the object Javascript -


how can value input , store object? when button clicked value input - should stored in object. thank lot in advance

var testobject = { 'name': nameofbook.value,                       'author': nameofauthor.value,                       'year': year.value                  };  console.log(testobject);
    <input type="text" id="nameofbook" required="" placeholder="book name" />      <input type="text" id="nameofauthor" required="" placeholder="athor name" />      <input type="text" id="year" required="" placeholder="add year" />       <button id="addder" type="button">storeemail</button>

here's working jsfiddle you.

the relevant js code here. using id tags on html elements, got of elements , stored them variables. next, added event listener on button, , on click, push relevant value of each element testobject.

var testobject = [];  const button = document.getelementbyid("addder");  const name = document.getelementbyid("nameofbook"); const author = document.getelementbyid("nameofauthor"); const year = document.getelementbyid("year");   button.addeventlistener("click", function() {   testobject.push({     name: name.value,     author: author.value,     year: year.value   })   console.log(testobject) }) 

https://jsfiddle.net/991jqomq/


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -