Entry
how do I search for a particular string inside the options of a select object??
Feb 3rd, 2005 03:54
jsWalter, Mir Ali,
Here are the 3 function I use to do just that!
This is part of a larger, more extensive, SELECT Object Class
Walter
====================================
// {{{ findOptionByValue()
/**
* Method public void findOptionByValue( object, string )
*
* Find an Option Item of a Select Object by its OPTION Value,
* or if OPTION does not have a value, its Display Text
*
* @name findOptionByValue
* @author Walter Torres <jsWalter@torres.ws>
*
* @category SELECT Object Extentions
* @uses findOption()
*
* @access public
* @since v1.3
*
* @param mixed Reference to Select Form Object, or its ID name
* @param string Select OPTION Value to search for
* @return mixed integer of OPTION index, or Boolean FALSE if not
found
*
*/
function findOptionByValue($objSelect, $strValue)
{
// Straight pass-through
return findOption($objSelect, $strValue, 'value' );
};
// }}}
// {{{ findOptionByText()
/**
* Method public void findOptionByText( object, string )
*
* Find an Option Item of a Select Object by its Display Text
*
* @name findOptionByText
* @author Walter Torres <jsWalter@torres.ws>
*
* @category SELECT Object Extentions
* @uses findOption()
*
* @access public
* @since v1.3
*
* @param mixed Reference to Select Form Object, or its ID name
* @param string Select OPTION Display Text to search for
* @return mixed integer of OPTION index, or Boolean FALSE if not
found
*
*/
function findOptionByText($objSelect, $strText)
{
// Straight pass-through
return findOption($objSelect, $strText, 'text' );
};
// }}}
// {{{ findOption()
/**
* Method public void findOption( object, string, string )
*
* Find an Option Item of a Select Object by its Display Text
* or OPTION value
*
* @name findOption()
* @author Walter Torres <jsWalter@torres.ws>
*
* @category SELECT Object Extentions
* @uses none
*
* @access public
* @since v1.3
*
* @param mixed Reference to Select Form Object, or its ID name
* @param string What to look for
* @param string Where to look for it, 'text' or 'value'
* @return mixed integer of OPTION index, or Boolean FALSE if not
found
*
*/
function findOption($objSelect, $strNeedle, $strSearch)
{
/**
* Default return value
*
* Returns the index of a found OPTION Item or boolean upon
failure
* Default value is set at FALSE
*
* @var mixed $_found index of a found OPTION Item or not
* @access private
* @static
*/
var $_found = new Boolean( false );
// If $objSelect is a nothing [''], convert to a NULL
if ( $objSelect == '' )
$objSelect = null;
// If $objSelect is a string, convert to Object Reference
// Then see if Object Reference exists
else if ( ( typeof ( $objSelect ) ) == 'string' )
$objSelect = document.getElementById($objSelect);
// If we have nothing to deal with, just bale...
if ( ( $objSelect ) && ( $strNeedle ) &&
( ( $strSearch == 'text' ) || ( $strSearch ==
'value' ) ) )
{
for (var $i = 0; $i < $objSelect.options.length; $i++)
{
if ( $strSearch == 'value' )
$objHayStack = $objSelect[$i].value;
else if ( $strSearch == 'text' )
$objHayStack = $objSelect[$i].text;
if ( $objHayStack == $strNeedle )
{
$_found = parseInt ( $i ); // We found a match
break;
}
}
}
// Send back what we have
return $_found;
};
// }}}