Skip to content

Commit

Permalink
added some files to the repo
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmkrueger committed Feb 22, 2011
1 parent 9692ede commit 4707c8f
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README
@@ -0,0 +1,14 @@
This is a collection of javascript files that I use frequently and almost always add to new projects. The included files are:

1.)css_browser_selector.js
-this is great for making rules like:
.ie7 #form_box
-very useful!

2.)input-placeholder-text.js
-only webkit browsers allow placeholder text for form elements. This turns it into a javascript method.
-include the classes shown at the top of the javascript file!

3.)my_functions.js
-These are functions that I have written to do specific things I reuse:
+if there is no .getElementsByClassName, create the function(pull it togeather ie!)
8 changes: 8 additions & 0 deletions css_browser_selector.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

161 changes: 161 additions & 0 deletions input-placeholder-text.js
@@ -0,0 +1,161 @@
/*
Project: Input Placeholder Text
Title: Automatic population of form fields with contents of title attributes
Author Jon Gibbins (aka dotjay)
Created: 13 Aug 2005
Modified: 26 Oct 2009
Notes:
Add the following classes to text inputs or textareas to get the
desired behaviour:
auto-select
Will pre-populate the input with the title attribute and select the
text when it receives focus.
auto-clear
Will pre-populate the input with the title attribute and clear the
text when it receives focus. Note: if auto-select and auto-clear
are set, auto-select takes precedence.
populate
Will just populate the input with the title attribute.
NB: A class name of "placeholder" is set on form inputs that have
placeholder text in them. This allows you to style the inputs
differently when there is user input versus placeholder text, e.g.
input.placeholder,
textarea.placeholder{
color: #777;
}
Potential additions:
Add in handling of default text if an initial value is detected (line
60, line 93, etc) using something like:
if (!el.defaultValue) continue;
el.onfocus = function() {
if (this.value == this.defaultValue) this.value = "";
}
el.onblur = function() {
if (this.value == "") this.value = this.defaultValue;
}
*/

function hasClass(el,cls) {
return el.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(el,cls) {
if (!this.hasClass(el,cls)) el.className += " "+cls;
}
function removeClass(el,cls) {
if (hasClass(el,cls)) {
var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
el.className = el.className.replace(reg,' ');
}
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}

function initFormPlaceholders() {

if (!document.getElementsByTagName) return true;

ourForms = document.getElementsByTagName('form');

// go through each form
var numForms = ourForms.length;
for (var i=0;i<numForms;i++) {

// go through each form element
var numFormElements = ourForms[i].elements.length;
for (var j=0;j<numFormElements;j++) {

var el = ourForms[i].elements[j];

// ignore submit buttons
if (el.type == "submit") continue;

// if we got a text type input or textarea
if ((el.type == "text") || (el.type == "textarea")) {
// only populate if we want it to
// note: might want title attribute but no pre-population of inputs
var ourClassName = el.className;
if (ourClassName.match('auto-select') || ourClassName.match('auto-clear') || ourClassName.match('populate')) {
// only populate if empty
if (el.value == '') {
addClass(el, "placeholder");
el.value = el.title;
}
}

// add auto select if class contains auto-select
// note: else if below so auto-select takes precedence (assuming select is better than clear)
if (el.className.match('auto-select')) {
el.onfocus = function () {
if (this.value == this.title) {
removeClass(this, "placeholder");
this.select();
}
}
if (el.captureEvents) el.captureEvents(Event.FOCUS);

el.onblur = function () {
if (this.value == '') {
this.value = this.title;
}
if (this.value == this.title) {
addClass(this, "placeholder");
}
}
if (el.captureEvents) el.captureEvents(Event.BLUR);
}

// add auto clear if class contains auto-clear
else if (el.className.match('auto-clear')) {
el.onfocus = function () {
if (this.value == this.title) {
removeClass(this, "placeholder");
this.value = '';
}
}
if (el.captureEvents) el.captureEvents(Event.FOCUS);

el.onblur = function () {
if (this.value == '') {
this.value = this.title;
}
if (this.value == this.title) {
addClass(this, "placeholder");
}
}
if (el.captureEvents) el.captureEvents(Event.BLUR);
}
}

}

}

}

addLoadEvent(initFormPlaceholders);
21 changes: 21 additions & 0 deletions my_functions.js
@@ -0,0 +1,21 @@
// Place your application-specific JavaScript functions and classes here

onload=function(){
if (document.getElementsByClassName == undefined) {
document.getElementsByClassName = function(className)
{
var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
var allElements = document.getElementsByTagName("*");
var results = [];

var element;
for (var i = 0; (element = allElements[i]) != null; i++) {
var elementClass = element.className;
if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
results.push(element);
}

return results;
}
}
}

0 comments on commit 4707c8f

Please sign in to comment.