views:

49

answers:

5

Hi. I'm trying to understand JQ better. I'm calling an JQ object

$(".FamiliesList li li span[class!='']").prev().find('option:selected')

this returns back to me an array of all the options that their span parent's brother has a classname.

[option, option]

Now- I want to return back an array of the option's values

$(".FamiliesList li li span[class!='']").prev().find('option:selected').attr('value')

this returns back to me only the first child value, and a full array of the values.

Why?

I would appreciate to receive help and understand jq better :)

Thanks.

+1  A: 

try

$(".FamiliesList li li span[class!='']").prev().find('option:selected').each(function () { return $(this).attr('value'); });`
W_P
A: 

Wouldn't you be looking for something like

$(".FamiliesList li li span[class!='']").prev().find('option:selected').each(function() {
  return this.attr('value');
});

Afaik, the attr() method doesn't work on multiple objects, so you'd need to call each() on the returned objects.

Jeriko
should be `$(this)` instead of `this`
Aaron Mc Adam
The shortcut method `.val()` should be mentioned here too
Aaron Mc Adam
+5  A: 

The best answer I can offer is, "that's just the way the API works". I agree with you that things like "attr" and "val" would be more consistent if they returned arrays (at least in the case that a selector matches multiple elements).

You can get that effect with $.map if you want:

var attrs = $.map($('div.something'), function(element) {
  return $(element).attr('whatever');
});

Now "attrs" will be an array. You could also write your own function.

In any case, it's important to note that there are arrays, and then there are "jQuery objects". It's never really going to make sense for "attr" or "val" (or anything like that) to be used in the middle of a set of jQuery operations, if you think about it.

Pointy
Thank you and thanks everyone in this thread for answering me.
Shlomi.A.
Didn't get u man too much in ur last paragraph. Why it doesn't make sense? this returns back an object $(".FamiliesList li li span[class!='']"). its an attribute selector. why thats ok?after all I'm using jq to match DOM elements, isn't for that she was created?
Shlomi.A.
A "jQuery object" is a specialized array that supports all the jQuery functions, with the elements of the "array" part of the object being a set of matched DOM nodes. It doesn't really make sense, given the overall architecture of jQuery as it stands, to have a jQuery object where the array values are something like "attr" results.
Pointy
+1  A: 

You can do this using .map(), like this:

var values = $(".FamiliesList li li span[class!='']").prev()
               .find('option:selected').map(function(function() {
                 return $(this).attr('value');
               }).get();

This would get [value1, value2, value3], an array of the values of the selected options.

Nick Craver
+2  A: 

Actually, $(selector) does not return an array. The result of $(selector) is a jQuery object, which is defined as a "set of matched elements". This set can contain 0, one or more "elements", but jQuery itself remains a single object. Just a box that can hold nothing or something.

So, if $(...) doesn't return an array, what would be reason for attr() or val() to return it? That's why property getters always (?) return the properties of the first element in the jQuery object they are applied to.

stereofrog
Got smarten here. I still think that the inconsistency is strange. if I can return an selector's object by its properties, why I cannot match by method?like attr() will return first child, but [attr=*] won't?
Shlomi.A.