views:

25

answers:

1

i want to find all div attributed value matched smaller or higher numeric value.

its worked me only eq value=444 (example div[ref=444],[ref=333]....)

var $filteredData = $data.find('div[ref=444]' );

but i want to compare find <444 ( find div attribute value smaller <444) or >444 (find div attribute value higher >444) in all div attribute value

how can find or select all div attr value smaller or higher thanks for all

+2  A: 

Yes, attributes are strings, there is no selector to treat them as numbers. You would have to parse and filter manually:

$filteredData= $data.find('div').filter(function() {
    return parseInt($(this).attr('ref'), 10)<444;
});

(But what's ref? If this is HTML content, that's not a standard attribute. Avoid custom HTML attributes; if you must use them, use HTML5 data- attributes. But other methods, such as putting the information in a className, might be better, depending on what you're doing.)

bobince
bobince - Is the `parseInt` really needed?
patrick dw
@patrick: Indeed not, in this case: the comparison between `String` and `Number` has an implicit conversion of `String` to `Number`, so `parseInt` would only be needed if there was something else in the attribute value that caused the implicit cast to give `NaN`. For example if the value was `123a` or some other not-wholly-a-number value that `parseInt`'s more lenient parsing would allow. Really I just wanted to be explicit: although the case of comparing `String` against `Number` is defined to work, there are many other cases where using a string containing a number won't work as expected.
bobince
bobince - Explicit/safe is good. Thanks for the response. +1 :o)
patrick dw