JMosca77 Posted October 10, 2006 Share Posted October 10, 2006 Hi,Is there a way to make a field read only using CSS? I need to prevent users from changing field values after a form has been submitted. I think CSS would be the best way to accomplish this without having to create duplicate forms with Read Only fields, but I can't get it to work.Thanks for any help. Link to comment Share on other sites More sharing options...
scott100 Posted October 10, 2006 Share Posted October 10, 2006 Not css but javascript, if you get the id of the element you can disable it.http://www.w3schools.com/js/tryit.asp?file...select_disabled Link to comment Share on other sites More sharing options...
JMosca77 Posted October 11, 2006 Author Share Posted October 11, 2006 Thanks for the info! Do you know of any way in Javascript I can write a loop that goes through all the fields on a form (without having to hard code each field name) and disable them? Link to comment Share on other sites More sharing options...
aspnetguy Posted October 11, 2006 Share Posted October 11, 2006 var fields = document.getElementsByTagName('input');for(var i=0;i<fields.length;i++){ if(fields[i].type == 'text') fields[i].disabled = true;} try that Link to comment Share on other sites More sharing options...
scott100 Posted October 11, 2006 Share Posted October 11, 2006 sure do: elm=document.forms[0];for (i=0;i<elm.length;++ i){ elm[i].disabled=true;} Link to comment Share on other sites More sharing options...
aspnetguy Posted October 11, 2006 Share Posted October 11, 2006 sure do:elm=document.forms[0];for (i=0;i<elm.length;++ i){ elm[i].disabled=true;} will that really work? elm is just a form object, is the form object just a collection of the elements in the form?If it does work i did not know that, Link to comment Share on other sites More sharing options...
JMosca77 Posted October 11, 2006 Author Share Posted October 11, 2006 will that really work? elm is just a form object, is the form object just a collection of the elements in the form?If it does work i did not know that, Both options worked, however ASPNETGUY's worked better for me because it made all fields (text, radio buttons, etc.) inactive instead of just fields.Thanks guys! Link to comment Share on other sites More sharing options...
aspnetguy Posted October 11, 2006 Share Posted October 11, 2006 I just thought of this. If you have password fields you will want to modify the if statement to look like this if(fields[i].type == 'text' || fields[i].type == 'password') Link to comment Share on other sites More sharing options...
scott100 Posted October 11, 2006 Share Posted October 11, 2006 will that really work? elm is just a form object, is the form object just a collection of the elements in the form?If it does work i did not know that, Yeah it does work, it's just shorthand for the second one.elm.disabled=true;elm.elements.disabled=true;but yeah yours was a bit more detailed so it'd work better cause mine would disable everthing (like the submit button ) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now