views:

56

answers:

3
var selValues = {};
selValues['234'] = $('#asd').val();
selValues['343'] = function () { var el = ''; $('#asd input[@type=checkbox]:checked').each(function() { el += $(this).val() + '|'; }); return el; } };

here's the explanation:

im creating a key-value array where it extracts different values from DOM objects. The last array that you see in the example actually tries to extract checked items in a checkbox list. I tried to delegate the loop and return a delimited string of all checked values, but it's not working.

+1  A: 

A mapping is probably a better solution here:

var el = $('#asd input:checkbox:checked').map(function(){
    return $(this).val();
}).get().join('|');
jAndy
If that's an equivalent of what they're trying to do, I like this approach way better :)
theIV
had a few syntax errors, it worked. thanks man!
Martin Ongtangco
@Martin: welcome, but what are the syntax errors?
jAndy
my own mistakes, accidentally added an apostrophe between the .get() and .join('|').
Martin Ongtangco
A: 

If I'm understanding your question correctly, the problem you are running up against is that you are merely storing a function in selValues['343'], not evaluating it.

You could try selValues['343'] = function () { var el = ''; $('#asd input[@type=checkbox]:checked').each(function() { el += $(this).val() + '|'; }); return el; } }(); (notice the parentheses at the end) which should evaluate your function and store the result in selValues['343'].

theIV
A: 

Seems to work for me: http://jsfiddle.net/HM4zD/

Strelok
cool link. if you do a alert(JSON.stringify(selValues));, the result to speak otherwise.
Martin Ongtangco