Problem removing readOnly in IE

I am having trouble with the following code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test</title>
</head>
<body>
<input value='test' type='text' readonly='readonly'
onfocus='this.readOnly = false;'
onblur='this.readOnly = true;'></input>
</body>
</html>

In FF, this works as I would expect. When clicking on the input, the readOnly
property is set to false and the focus is given to the element, with the
cursor placed after the last character.

In IE, the readOnly property is set to false, but the element isn't actually
given focus. Instead, I have to click on the input a second time in order to
actually input text with the keyboard.

Is there a way to make this work the same in IE as it does in FF? I have
tried things like calling this.focus() and this.click() after setting
readOnly to false, but they have no effect.

Any help would be greatly appreciated.

Thank you,
LVL 3
Abdu_AllahAsked:
Who is Participating?
 
ronan1979Connect With a Mentor Commented:
you need to set/delet the attribute readonly

 document.myForm.myInput.setAttribute('readonly','readonly');
 document.myForm.myInput.removeAttribute('readonly');

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test</title>
</head>
<body>
<form name="myForm">
<input name="myInput" value='setA' type='text'></input>
<input value='test' type='text' readonly=''></input>
<input value='test' type='text' ></input>


<script>
fnReadOnly = function () {
  document.myForm.myInput.setAttribute('readonly','readonly');
}
fnReadWrite = function () {
  document.myForm.myInput.removeAttribute('readonly');
}

init = function () {
document.myForm.myInput.onfocus = fnReadWrite;
document.myForm.myInput.onblur = fnReadOnly;
if(document.myForm.myInput.focused) {
  fnReadOnly();
  }

}

window.onload = init;

</script>

</body>
</html>

Open in new window

0
 
ronan1979Commented:
note:
the simple fact you have the attribute readonly even with no value will make it read only

readonly has only one value possible : 'readonly' see http://www.w3.org/TR/html401/interact/forms.html
= read only =
<input value='test' type='text' readonly=''></input>
=read and write =
<input value='test' type='text' ></input>
0
 
Abdu_AllahAuthor Commented:
It does not work!
0
 
ronan1979Commented:
for IE

document.myForm.myInput.readOnly = true; // or false

0
 
ronan1979Commented:
i dont understand what you are trying to do.

even if redOnly is set, you can modify the value with a javascript code:

 document.myForm.myInput.value="hello";

readOnly is only concern for keyboard/mouse modification.

in your case, readOnly seems have no meaning
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.