Abdes
Abdes

Reputation: 986

get value array of object in jquery

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 ?

Upvotes: 0

Views: 60

Answers (1)

Suren Srapyan
Suren Srapyan

Reputation: 68635

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

Upvotes: 3

Related Questions