Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Readonly

Status
Not open for further replies.

sand133

Programmer
Jun 26, 2004
103
GB
I cant seem to make these readonly any ideas, thanks





for(i=0; i<document.forms[0].elements.length; i++)
{

{
document.forms[0].elements.readOnly = true;

}
 
Try this..

Code:
for(i=0; i<document.forms[0].elements.length; i++)
{
     
     {
          document.forms[0].elements[i].setAttribute('readOnly','readOnly');

     
}


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
that works great but for dropdowns it does not work, any ideas?
 
no , ah wait a minute, i'm not sure select lists have readonly attribute!

i beleive it's disabled="disabled"

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
This would satisfy in most cases apart from possible conflicting handling. In any case, this illustrates how.
[tt]
function doit3() {
var celem=document.forms[0].elements;
for (var i=0;i<celem.length;i++) {
var oelem=celem;
switch (oelem.type) { //oelem.type.toLowerCase() if in doubt
case "select-one" :
var n=this.selectedIndex;
oelem.onchange=function(n) { this.selectedIndex=n;};
break;
case "select-multiple" :
var a=new Array();
for (var j=0;j<oelem.options.length;j++) {
if (oelem.options[j].selected) {
a.push(j);
//a[a.length]=j; //if push is not supported
}
}
if (a.length!=0) {
oelem.onchange=function () {
for (var m=0;m<this.options.length;m++) {
this.options[m].selected=new RegExp("^"+m+"(,|$)|,"+m+",|(^|,)"+m+"$").test(a.join(","));
}
}
}
break;
case "radio" :
oelem.onclick=function() {this.checked=!this.checked;};
break;
case "checkbox" :
oelem.onclick=function() {return false;};
break;
case "reset" :
oelem.onclick=function() {return false;};
break;
case "submit" : //you may not want to make it readOnly - that means the form won't submit
//oelem.onclick=function() {return false;};
break;
default : //text, textarea, password, hidden
oelem.readOnly=true;
}
}
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top