How to get a Object from inputs in vanilla javascript

后端 未结 3 546
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 22:35

For exemple, i have 3 inputs





        
3条回答
  •  旧巷少年郎
    2021-01-16 23:35

    You can loop through the values using a forEach and update the object.
    Note: Could also use reduce to directly create the object

    const obj = {};
    const inputs = document.querySelectorAll("input");
    inputs.forEach(({ name, value }) => {
     obj[name] = value
    })
    
    console.log(obj);
    
    
    

提交回复
热议问题