0

I have four input

<input type="text" class="kal" name="data[tex1]">
<input type="text" class="kal" name="data[tex2]">
<input type="text" class="kal" name="data[tex3]">
<input type="text" class="kal" name="data[tex4]">

Here is my jquery code :

var map = {};
     $(".kal").each(function() {
    map[$(this).attr("name")] = $(this).val();
    });

     console.log(map);

    });

Now in console, I receive the result like this:

Object {data[tex1]: "ALI", data[tex2]: "JOHN", data[tex3]: "18", data[tex4]: "MOROCCO"}

How I can get values this object using jquery ?

1
  • I don't understand... you are getting the values by jQuery. Commented Mar 14, 2017 at 13:44

1 Answer 1

3

You can access properties of object via bracket [] or . syntax. In your case your map is an object, but you can access it's properties via only [] syntax, because JavaScript Engine will give you error when you try to access with . syntax,because those names are not valid for property for using with . syntax.

Access via

map['data[tex1]']

Example

var map = {
   'data[tex1]': 'AAA',
   'data[tex2]': 'BBB',
   'data[tex3]': 'CCC',
}; // After all your object will have this look

console.log(map['data[tex1]']);
// console.log(map.data[tex1]); this syntax will give you an error

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

1 Comment

Won't it be map['data[tex1]'], since the input names contain the string data[texX] ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.