Never been to CodeSnippets 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 (or not, you can keep them private!)

About this user

Get Selected Option Text From HTML Select List



var select_list_field = document.getElementById('field_id');
var select_list_selected_index = select_list_field.selectedIndex;

var text = select_list_field.options[select_list_selected_index].text;
var value = select_list_field.value;

Time Difference

Get the difference between 2 times in PHP

$start = "09:00";
$finish = "10:30";
 
 
$tempstart=explode(":",$start);
$tempEnd=explode(":",$finish);
$tt=(($tempstart[0]*60)+$tempstart[1])+2400; 
$pp=(($tempEnd[0]*60)+$tempEnd[1]);
$difference=$tt-$pp;
$difference=$difference%2400;
$difftime=intval($difference/60).":".intval($difference%60);

Javascript String Replace

just like php's str_replace

var visitorName = "Chuck";
var myOldString = "Hello username! I hope you enjoy your stay username.";
var myNewString = myOldString.replace("username", visitorName);

document.write("Old string =  " + myOldString); 
document.write("<br />New string = " + myNewString);

Fire Event With Listener

If you need to fire off an event but it is currently handled by an event listener.

more information here http://www.howtocreate.co.uk/tutorials/javascript/domevents


var fireOnThis = document.getElementById('someID');
if( document.createEvent ) {
  var evObj = document.createEvent('MouseEvents');
  evObj.initEvent( 'mousemove', true, false );
  fireOnThis.dispatchEvent(evObj);
} else if( document.createEventObject ) {
  fireOnThis.fireEvent('onmousemove');
}

Javascript Event Listeners

Add event listeners for all browsers at once.
##
Not my own code but works like a charm!
##
//*** This code is copyright 2003 by Gavin Kistner, gavin@refinery.com
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
//*** Reuse or modification is free provided you abide by the terms of that license.
//*** (Including the first two lines above in your source code satisfies the conditions.)


//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted
//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false);

function AttachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
} 

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc){
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}
function MyFireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}

Javascript Trim Functions

Examples of full trim, left trim, and right trim for javascript.

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

// example of using trim, ltrim, and rtrim
var myString = " hello my name is ";
alert("*"+trim(myString)+"*");
alert("*"+ltrim(myString)+"*");
alert("*"+rtrim(myString)+"*");

PHP Bitwise Comparison

Make a bitwise comparison in php.

if(($bit_comparison & $bit) == TRUE)
{
    // do something
}

Get Arguments Passed To A Function

Javascript function can accept arguments which are not specified in the parameter list

function someFunc()
{
var firstArg = someFunc.arguments[0];
var secondArg = someFunc.arguments[1];
....
}

Create Javascript Object

Use objects for lists

var obj = {
"elem": document.getElementById('id'),
"description": "here's a description"
};

Bitwise SQL Comparison

// Do a bitwise comparison in mysql

select * from table where (bit_sum & bit) = bit