| | 1 | (function() { |
| | 2 | var placeholders = [], |
| | 3 | inputs = document.getElementsByTagName("input"), |
| | 4 | textareas = document.getElementsByTagName("textarea"), |
| | 5 | i = 0; |
| | 6 | |
| | 7 | // Get text inputs with a placeholder attribute set |
| | 8 | for (i = 0 ; i < inputs.length ; i++) { |
| | 9 | if (null !== inputs[i].getAttribute("type") && "text" === inputs[i].getAttribute("type").toLowerCase() && null !== inputs[i].getAttribute("placeholder") && "" !== inputs[i].getAttribute("placeholder")) { |
| | 10 | placeholders.push(inputs[i]); |
| | 11 | } |
| | 12 | } |
| | 13 | |
| | 14 | // Get textareas with a placeholder attribute set |
| | 15 | for (i = 0 ; i < textareas.length ; i++) { |
| | 16 | if (null !== textareas[i].getAttribute("placeholder") && "" !== textareas[i].getAttribute("placeholder")) { |
| | 17 | placeholders.push(textareas[i]); |
| | 18 | } |
| | 19 | } |
| | 20 | |
| | 21 | // Show placeholders |
| | 22 | for (i = 0 ; i < placeholders.length ; i++) { |
| | 23 | if (null === placeholders[i].getAttribute("value") || "" === placeholders[i].getAttribute("value")) { |
| | 24 | placeholders[i].value = placeholders[i].getAttribute("placeholder"); |
| | 25 | placeholders[i].style.color = "#a9a9a9"; |
| | 26 | } |
| | 27 | placeholders[i].onfocus = function() { |
| | 28 | if (this.value === this.getAttribute("placeholder")) { |
| | 29 | this.value = ""; |
| | 30 | } |
| | 31 | this.style.color = ""; |
| | 32 | }; |
| | 33 | placeholders[i].onblur = function() { |
| | 34 | if (null === this.value || "" === this.value) { |
| | 35 | this.value = this.getAttribute("placeholder"); |
| | 36 | } |
| | 37 | }; |
| | 38 | } |
| | 39 | })(); |
| | 40 | No newline at end of file |