dev.kanngard.net make sure you visit my new blog at: johankanngard.net

Extract the value or alias from a alias/value pair with JavaScript

by Johan Känngård / [JavaScript] / 2004-08-26 / #81


A trivial task, but handy enough to be mentioned here. You could use them like this:

var s='First|1';
alert('value is:'+getValue(s));
alert('alias is:'+getAlias(s));
/**
 * Returns the value in a value / alias pair formatted
 * like "value|alias".
 *
 * @param s the String to get the value (the part before
 * the pipe) from.
 * @return the value of the specified value / alias pair.
 */
function getValue(s){
	return s.split('|')[0];
}

/**
  * Returns the alias in value / alias pair.
  *
  * @param s the String to get the alias (the part after
  * the pipe) from.
  * @return the alias of the specified value / alias pair.
  */
function getAlias(s){
	a=s.split('|')
	return a[a.length-1];
}