Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
160
rated 0 times [  164] [ 4]  / answers: 1 / hits: 27477  / 11 Years ago, sat, january 4, 2014, 12:00:00

I have a input elements in html with two important attributes: id, and parentElementId.



I want to create a map/dictionary that looks like this: id : parentElementId.



var parent = $(.people-autocomplete).map( function(){ return $(this).attr('id')+':'+$(this).attr('parent'); }).get() ;


for know I'm putting the values into a string, which I parse later on in the code. I presume there is a more elegant solution than this.


More From » jquery

 Answers
6

Use an object:



var obj = {};
$(.people-autocomplete).each(function() {
obj[$(this).attr('id')] = $(this).attr('parent');
});


You can then access the parent of a specific id:



var parent = obj.idName;


or through a string:



var idStr = 'idName';
var parent = obj[idStr];


And you can loop through:



for (idStr in obj) {
var parent = obj[idStr];
}

[#73377] Friday, January 3, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cindyanyssam

Total Points: 483
Total Questions: 94
Total Answers: 100

Location: Barbados
Member since Sat, May 28, 2022
2 Years ago
;