I've been using a JavaScript placeholder script, to support IE placeholders.
Works fine for input type = text. But how do I write an addition to the script to support textarea?
My code is :
function activatePlaceholders() {
var detect = navigator.userAgent.toLowerCase();
if (detect.indexOf("safari") > 0) return false;
var inputs = document.getElementsByTagName("input");
for (var i=0;i<inputs.length;i++) {
if (inputs[i].getAttribute("type") == "text") {
if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
inputs[i].value = inputs[i].getAttribute("placeholder");
inputs[i].onclick = function() {
if (this.value == this.getAttribute("placeholder")) {
this.value = "";
}
return false;
}
inputs[i].onblur = function() {
if (this.value.length < 1) {
this.value = this.getAttribute("placeholder");
}
}
}
}
}
}
window.onload=function() {
activatePlaceholders();
}
Thanks in advance.
I've been using a JavaScript placeholder script, to support IE placeholders.
Works fine for input type = text. But how do I write an addition to the script to support textarea?
My code is :
function activatePlaceholders() {
var detect = navigator.userAgent.toLowerCase();
if (detect.indexOf("safari") > 0) return false;
var inputs = document.getElementsByTagName("input");
for (var i=0;i<inputs.length;i++) {
if (inputs[i].getAttribute("type") == "text") {
if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
inputs[i].value = inputs[i].getAttribute("placeholder");
inputs[i].onclick = function() {
if (this.value == this.getAttribute("placeholder")) {
this.value = "";
}
return false;
}
inputs[i].onblur = function() {
if (this.value.length < 1) {
this.value = this.getAttribute("placeholder");
}
}
}
}
}
}
window.onload=function() {
activatePlaceholders();
}
Thanks in advance.
Share Improve this question asked Jan 10, 2012 at 11:13 StuBlackettStuBlackett 3,85915 gold badges71 silver badges116 bronze badges2 Answers
Reset to default 4textarea is not a input type. It is a tag in itself. Example
<textarea rows=10 placeholder="Enter text here"></textarea>
In this case, your code can be
var inputs = document.getElementsByTagName("textarea");
inputs[0].setAttribute('placeholder','New placeholder');
Hope it helps
Simply query for text area elements
function activatePlaceholders() {
var detect = navigator.userAgent.toLowerCase();
if (detect.indexOf("safari") > 0) return false;
var inputs = document.getElementsByTagName("textarea");
for (var i=0;i<inputs.length;i++) {
if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
inputs[i].value = inputs[i].getAttribute("placeholder");
inputs[i].onclick = function() {
if (this.value == this.getAttribute("placeholder")) {
this.value = "";
}
return false;
}
inputs[i].onblur = function() {
if (this.value.length < 1) {
this.value = this.getAttribute("placeholder");
}
}
}
}
}
window.onload=function() {
activatePlaceholders();
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745362430a4624421.html
评论列表(0条)