admin管理员组文章数量:1426004
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();
}
本文标签: javascriptPlaceholdersFor textarea inputStack Overflow
版权声明:本文标题:javascript - Placeholders - For textarea input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745362430a2655356.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论