views:

41

answers:

4

I have a select list with multiple items which I want to be able to filter by the text entered into some text field. This is pretty simple if you just do a js contains while iterating over each option. There are however in some cases a lot of options, so I employed jQuery:

var results = $("#myList options").find("option[text*=" + someVal.toUpperCase() + "]");

This works fine in IE, but not in Chrome and I simply cannot find out why (Firebug doesn't work at all on this page, but that's an aside.

Perhaps it's because "text" doesn't actually exist in the option html, but no searching by value fails too. Any ideas?

A: 

How many #myList elements are there in the page?

danp
please post this as a comment above before down-voting start in you... ;)
Reigel
An interesting question and one which I am embarrassed to answer truthfully. About 13,000.......... yes.. I am going to trash the approach and use some kind of paging this which retrieves the data ajaxly. I just really wanna know why this approach doesnt work! I managed to get teh 13k records rendering very quickly, storing them and in IE even filtering them pretty quickly.. odd
Mr AH
it doesn't work because `#myList options` will match nothing. Use `option` instead. `option[text*=` will possibly not match in every browser. Check for `option[value*=` instead.
jAndy
true, should be a comment, I blame monday mornings, sorry :p
danp
+2  A: 

there are many errors here...

var results = $("#myList options").find("option[text*=" + someVal.toUpperCase() + "]");
 //                         ^              ^

the above code, will get all options' (option with an s). then look for children option containing a text attribute with someVal.toUpperCase() value.......

I guess you would want like this,

var results = $("#myList option").map(function(){
                  if ($(this).text().indexOf(someVal)!= -1 ) { return this; }
              }).get();
Reigel
+2  A: 

use jQuerys .filter() instead.

var results = $("#myList").children('option').filter(function(){ 
    return ( $(this).val().indexOf(someVal.toUpperCase()) > -1 );
});
jAndy
+1  A: 

I've just tried with the following on chrome: http://jsbin.com/olida3/edit

correctly alerts "1" for 'test'

$('#myList option[value*='+val+']');

Alternatively you could also write it like

$('option[value*='+val+']', '#myList');
Ben Rowe