By muhammad


2019-10-09 14:13:34 8 Comments

I have a problem with some code. I need it to return a object like this:

{name: "Harry, email: "[email protected]", age: 22}
//age is 22 a number not '22' a string 

However it is getting returned like this:

{name: "Harry, email: "[email protected]", age: '22'} 

Here is my code

function formObj(elems) {
  let form = {};
  for(let elem of elems) {
    let key = elem.id;
    let val = elem.value;
    if(key && val)
      form[key] = val;
  }
  return form;
}

let elements = document.querySelectorAll('form *') 
formObj(elements);
<form>
    <label for='name'>name</label>
    <input type='text' id='name' value="Harry"/>

    <label for='email'>email</label>
    <input type='email' id='email' value="[email protected]"/>

    <label for='age'>age</label>
    <input type='text' id='age' value="22"/>
<form/>

Please help out

2 comments

@silleknarf 2019-10-09 14:16:34

If you change the type of the 'age' input to a number then you should get the desired result, e.g.

<input type='number' id='age' value="22" />

@Keith 2019-10-09 14:33:47

No, elem.value will still be a string. type='number' just effects how the browser shows the input.

@Nicolae Maties 2019-10-09 14:16:14

You can change the type of the key is age.

function formObj(elems) {
  let form = {};
  for(let elem of elems) {
    let key = elem.id;
    let val = elem.value;
    if(key && val)
      form[key] = key ==="age" ? Number(val) : val;
  }
  console.log(form)
  return form;
}

let elements = document.querySelectorAll('form *') 
formObj(elements);
<form>
    <label for='name'>name</label>
    <input type='text' id='name' value="Harry"/>

    <label for='email'>email</label>
    <input type='email' id='email' value="[email protected]"/>

    <label for='age'>age</label>
    <input type='text' id='age' value="22"/>
<form/>

Related Questions

Sponsored Content

84 Answered Questions

[SOLVED] How do I make the first letter of a string uppercase in JavaScript?

75 Answered Questions

[SOLVED] How can I convert a string to boolean in JavaScript?

  • 2008-11-05 00:13:08
  • Kevin
  • 1768490 View
  • 2290 Score
  • 75 Answer
  • Tags:   javascript

58 Answered Questions

[SOLVED] How to replace all occurrences of a string?

30 Answered Questions

[SOLVED] How to change an element's class with JavaScript?

  • 2008-10-12 20:06:43
  • Nathan Smith
  • 2449534 View
  • 2618 Score
  • 30 Answer
  • Tags:   javascript html dom

86 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 6040813 View
  • 7521 Score
  • 86 Answer
  • Tags:   javascript arrays

55 Answered Questions

[SOLVED] How do I check if an element is hidden in jQuery?

58 Answered Questions

[SOLVED] How do I redirect to another webpage?

41 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

3 Answered Questions

86 Answered Questions

[SOLVED] How do JavaScript closures work?

Sponsored Content