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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Readonly and not readonly 3

Status
Not open for further replies.

evergreean

Technical User
Feb 15, 2006
68
US

I am trying to create a JS where the user can check a checkbox to make the textarea readonly or unclick the checkbox to make it not readonly (write).


Here is the JS:
Code:
if(document.formOne.fieldInfo.checked)
{
       document.forms['myFormId'].myTextArea.setAttribute('readonly','readonly');
}
else if(!document.formOne.fieldInfo.checked)
{
      document.forms['myFormId'].myTextArea.setAttribute('readonly',true); 
      // also tried document.formOne.fieldtextarea.focus(); 
}

It does make the textarea readonly but once it is readonly I cant get it to switch back to write (Not readonly).

Please advise.
 
[1] use setAttribute and removeAttribute
[tt]
if(document.formOne.fieldInfo.checked)
{
document.forms['myFormId'].myTextArea.setAttribute('read[red]O[/red]nly','readonly'); [green]//2nd readonly more freedom[/green]
}
else [green]//[/green]if(!document.formOne.fieldInfo.checked)
{
document.forms['myFormId'].myTextArea.[blue]remove[/blue]Attribute('read[red]O[/red]nly');
// also tried document.formOne.fieldtextarea.focus();
}
[/tt]
[2] property assignment
[tt]
if(document.formOne.fieldInfo.checked)
{
document.forms['myFormId'].myTextArea.[blue]readOnly=true[/blue]; //true has some freedom
}
else //if(!document.formOne.fieldInfo.checked)
{
document.forms['myFormId'].myTextArea.[blue]readOnly=false[/blue]; //false has some freedom
// also tried document.formOne.fieldtextarea.focus();
}
[/tt]
 
I'm unsure of why your code isn't working, I couldn't get it to set the readOnly flag, or remove it. However, this code works in both firefox and IE. It might not be as sexy because it doesn't use the DOM method setAttribute, but it still works:

(don't forget to capitalize the O in readOnly)
Code:
<script type="text/javascript">

function blah(bool) {
   if(bool) {
      document.getElementById("ta").readOnly = true;
   }
   else {
      document.getElementById("ta").readOnly = false;
   }
}

</script>

<textarea id="ta"></textarea>
<input type="checkbox" onclick="blah(this.checked)" />

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Try this:
Code:
if(document.formOne.fieldInfo.checked)
{
       document.forms['myFormId'].myTextArea.setAttribute('read[COLOR=red]O[/color]nly','read[COLOR=red]O[/color]nly');
}
else if(!document.formOne.fieldInfo.checked)
{
      document.forms['myFormId'].myTextArea.setAttribute('read[COLOR=red]O[/color]nly',[COLOR=red]false[/color]); 
      // also tried document.formOne.fieldtextarea.focus(); 
}

The O must be capitalized when setting the attribute from within Javascript. It does not have to be so done just within the HTML which makes it confusing.
Also, your else statement was saying if the checkbox is NOT checked then set readOnly to true so you would be setting readOnly true in your if and else statements.


Stamp out, eliminate and abolish redundancy!
 
Also,

For Netscape 7.0 I have to put the JavaScript in the web page for it to work. For IE it does work when I call the JavaScript from a file.

Any solution for that or just leave it the way it is in the actual web page instead of calling it in a file.

Thanks
 
Are you storing the javascript in a .js file and then using an include statement?
I do not see why it would not work in Netscape but I am not familiar with the browser differences.
It might make a difference where the file is located on the server. On our IIS server I have to put my .js files in the HTML folder and cannot keep them in the script folder for some reason. There may be other issues at play there though.


Stamp out, eliminate and abolish redundancy!
 
Yes,

Stored in .js file in the same directory as the web page on an IIS Windows NT server.

Here is how it is called in the include statement:
Code:
<script language="JavaScript" src="theJS.js"></script>

I have never had a problem in past with calling .js file into my web pages.
 
Ensure that the path is correct for the file. Since you have not specified a directory, make sure that it is in the same directory as your .html file. If the js file is in the same directory as the .html file, then maybe try specifying the current directory: src="[!]./[/!]theJS.js" To be honest it shouldn't make a difference but might be worth a shot.

If you continue having problems with this I would suggest starting a new thread. It has moved outside of the scope of the original problem and you would probably get better suggestions if the title of the thread reflected the question that you desire to have answered.

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top