0

I have the following input DOM nodes:

<input type="text", name="name", "value": "Erik">
<input type="text", name="age", "value": "25">

I need to make object from the DOM above like the following:

{
    name: "Erik",
    age: 25
}

How to do it with jQuery?

EDIT: I need something like the following:

var result = $('input[name]').map(function (i, input) {
   var result = {};
   result[input.name] = input.value;       

   return result;
});

But $.map method returns array instead object;

1
  • it should look like this in first place, remove the quotes from value <input type="text", name="name", value: "Erik"> Commented May 10, 2014 at 12:01

2 Answers 2

2

Well, if using JQuery you have to reference them somehow. Given that you have no specific class applied, you can try something like this, which gathers all input values from your page:

var myValue = {};

$('input').each(function(){
    myValue[$(this).attr('name')] = $(this).val();
}

If you only want to get the names of specific input elements on your page, then you must refine the $('input') selector to something more specific.

Sign up to request clarification or add additional context in comments.

Comments

1

Here is how you can capture your values in an object

var obj = {
  name: $("input[name='name']").val(),
  age: $("input[name='age']").val()
};
console.log(obj);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.