Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

ryan bricolages.org

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

split value from units

this is a very basic script for seperating out values and units. this is helpful for changing values such as style.top or border.

if element.style.top => '15px'
splitUnit( element.style.top ) => [15,'px']

   1  function splitUnit(e)
   2  /*
   3  *	takes a string and seperates 'px or 'em' from it
   4  *
   5  *	could be streamlined to simply seperate the last two characters, but that could cause errors if used sloppily
   6  */
   7  {
   8  	if( e && ( length = e.search( /px/ ) ) ) {var eUnit = 'px'; var eValue = e.substr( 0, length );}
   9  	else if( e && ( length = e.search( /em/ ) ) ) {var eUnit = 'em'; var eValue = e.substr( 0, length );}
  10  	else return false;
  11  	
  12  	return new Array( eValue, eUnit );
  13  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS