Friday, April 28, 2006

Select Item by Text (using Prototype)

Function to select an item in a <select> element by its text value. Using the Prototype Library.


function selectItemByText ( selectElement, nameToSelect ) {
nameToSelect = nameToSelect.toLowerCase();
( $A($(selectElement).options ).detect (
function ( option ) {
return
option.text.toLowerCase()
== nameToSelect ;
}
) || new Object() ).selected = true;
}


I love prototype

No comments: