• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Making a page readonly

 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need a bit of javascript that will make every editable field on a page read only, except one. I would prefer not to enumerate through each field and explicitly make it read only. Any suggestions are welcome.
All best,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you just need to specify which form element you do not want to diable. You need to remove the _ from on_load
<script>
function LoadIt(){
for(i=0;i<document.forms.length;i++){
for(j=0;j<document.forms[i].elements.length;j++){
document.forms[i].elements[j].disabled='true';
}
}
document.FormName.ElementName.disabled=false; //Place element here
}
window.on_load=LoadIt;
</script>
Hope this helps
Eric
 
Max Habibi
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Eric,
Unfortunately, It's a bit of a hassle to disable the form elements, because then I would need to enable them again right before submitting(I'm working with Struts). Is there any way to intercept the clicks and tab events, and simply swallow them if they happen to be for any field other then the one I want?
All best,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I would do is this method I have shown which I changed below with a form submit. This method is the way I perfer since I do not have to put a functions to every form element on the page. Again move the _ from on_click and on_load.
<script>
function LoadIt(how){
for(i=0;i<document.forms.length;i++){
for(j=0;j<document.forms[i].elements.length;j++){
document.forms[i].elements[j].disabled=how;
}
}
if(how){
document.FormName.ElementName.disabled=false; //Place element here to be enabled
document.FormName.FS.disabled=false;
}
else{document.FormName.submit();}
}
function StartIt(){LoadIt('true');}
window.on_load=StartIt
</script>
<input type="button" value="Submit" Name="FS" on_click="LoadIt('false')>
or if you really want too it can be done like this. It works well and does not give the disabled look to the elements, but you need to add a tag to every element.

<script>
var Dissed=true;
function WatchIt(XX){
if(Dissed)XX.blur();
}
</script>
<input type="text" name="T1" value="A1">
<input type="text" name="T2" value="A1" onfocus="WatchIt(this)">
<input type="text" name="T3" value="A1" onfocus="WatchIt(this)">

And if for some reason you want the form elements to be enabled, all you need to do is set Dissed=false;
Eric
 
Max Habibi
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Eric, that's very helpful.
All best,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic