467,895 Members | 1,423 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,895 developers. It's quick & easy.

readonly attribute

Hi,

I am trying to set the readonly attribute of a text input tag dynamically
from javascript. The input element already exists in the form and I want to
make it readonly when a particular button is pressed. I have tried the
following in javascript;

function makeReadonly()
{
document.formName.inputTagName.setAttribute("reado nly", "true");
}

However this doesn't have any effect. Should this work or should I be doing
something else?

Many thanks,

Jimbo.
Mar 14 '06 #1
  • viewed: 5273
Share:
7 Replies
What browser are you using? A quick test of mine shows this works in
Firefox but not IE. Perhaps IE does not support the standards for
this?

P.S. - You should be setting id in your tags not name -- and then use
document.getElementById instead of document.formName.inputTagName.

Mar 14 '06 #2
Justin McConnell wrote:

Are you responding to something?
http://www.ah61.com/Quoting/googlequote.html
What browser are you using? A quick test of mine shows this works in
Firefox but not IE. Perhaps IE does not support the standards for
this?
Internet Explorer 6 does not support setAttribute in HTML mode IIRC.
P.S. - You should be setting id in your tags not name -- and then use
document.getElementById instead of document.formName.inputTagName.


Why? DOM 0 support is rather better than DOM 1 support isn't it?

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Mar 14 '06 #3
Jimbo said on 15/03/2006 6:25 AM AEST:
Hi,

I am trying to set the readonly attribute of a text input tag dynamically
from javascript. The input element already exists in the form and I want to
make it readonly when a particular button is pressed. I have tried the
following in javascript;

function makeReadonly()
{
document.formName.inputTagName.setAttribute("reado nly", "true");
setAttribute has not been consistently implemented, use:
document.formName.inputTagName.readonly = "true";
In general it is safer to access DOM object properties directly rather
than use setAttribute.

}

However this doesn't have any effect. Should this work or should I be doing
something else?


Yes, it *should* work, but that doesn't mean that it will. Because of
the inconsistent implementation of host features, it is normal to use
feature detection and guard against cases where what you are doing isn't
supported or doesn't do what you expect, e.g.:

var o;
if ( (o = document) && (o = o.formName) && (o = o.inputTagName) ){
o.readonly = true;
} else {
// didn't find element
}

And even though you may have set the readonly attribute of the DOM
object to true, it may not actually make it readonly (though I expect it
will in most browsers that get that far).
--
Rob
Mar 15 '06 #4
Yes, I'm responding to the root post in this thread. Is there a reason
why I should quote the previous post when its right there at the top of
the page?

Mar 15 '06 #5
RobG wrote :
Jimbo said on 15/03/2006 6:25 AM AEST:
Hi,

I am trying to set the readonly attribute of a text input tag
dynamically from javascript. The input element already exists in the
form and I want to make it readonly when a particular button is
pressed. I have tried the following in javascript;

function makeReadonly()
{
document.formName.inputTagName.setAttribute("reado nly", "true");
setAttribute has not been consistently implemented, use:
document.formName.inputTagName.readonly = "true";


It should be:

document.formName.inputTagName.readOnly = true;

http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-88461592

In general it is safer to access DOM object properties directly rather
than use setAttribute.

}

However this doesn't have any effect. Should this work or should I be
doing something else?
Yes, it *should* work, but that doesn't mean that it will. Because of
the inconsistent implementation of host features, it is normal to use
feature detection and guard against cases where what you are doing isn't
supported or doesn't do what you expect, e.g.:

var o;
if ( (o = document) && (o = o.formName) && (o = o.inputTagName) ){
o.readonly = true;


It should be:

o.readOnly = true;

http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-88461592
} else {
// didn't find element
}

And even though you may have set the readonly attribute of the DOM
object to true, it may not actually make it readonly (though I expect it
will in most browsers that get that far).

Gérard
--
remove blah to email me
Mar 15 '06 #6
Justin McConnell wrote :
Yes, I'm responding to the root post in this thread.
Your posting should restore some context, should refer to something that
you are replying to. Avoiding that defeats the purpose of archiving
postings in newsgroups and eventual searching of archived posts.

Consider a post which would only say:
"Yes, that's correct." ... but we never get to see what was the question.

Is there a reason why I should quote the previous post when its right there at the top of
the page?


Not everyone view/read posts in threads with google. Even google
recommends to quote material to which you are replying to.

"When you click 'Reply' under 'show options' to follow up an existing
article, Google Groups includes the full article in quotes, with the
cursor at the top of the article. Tempting though it is to just start
typing your message, please STOP and do two things first. Look at the
quoted text and remove parts that are irrelevant. Then, go to the BOTTOM
of the article and start typing there. Doing this makes it much easier
for your readers to get through your post. They'll have a reminder of
the relevant text before your comment, but won't have to re-read the
entire article.(...)"
Group Help
What's good 'netiquette' when posting to Usenet?
http://groups.google.com/support/bin...2348&topic=250
Gérard
--
remove blah to email me
Mar 15 '06 #7
Gérard,

Brilliant. That works for me.

Many thanks,

Jimbo.
"Gérard Talbot" <ne***********@gtalbot.org> wrote in message
news:47************@uni-berlin.de...
RobG wrote :
Jimbo said on 15/03/2006 6:25 AM AEST:
Hi,

I am trying to set the readonly attribute of a text input tag
dynamically from javascript. The input element already exists in the
form and I want to make it readonly when a particular button is pressed.
I have tried the following in javascript;

function makeReadonly()
{
document.formName.inputTagName.setAttribute("reado nly", "true");


setAttribute has not been consistently implemented, use:
document.formName.inputTagName.readonly = "true";


It should be:

document.formName.inputTagName.readOnly = true;

http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-88461592

In general it is safer to access DOM object properties directly rather
than use setAttribute.

}

However this doesn't have any effect. Should this work or should I be
doing something else?


Yes, it *should* work, but that doesn't mean that it will. Because of
the inconsistent implementation of host features, it is normal to use
feature detection and guard against cases where what you are doing isn't
supported or doesn't do what you expect, e.g.:

var o;
if ( (o = document) && (o = o.formName) && (o = o.inputTagName) ){
o.readonly = true;


It should be:

o.readOnly = true;

http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-88461592
} else {
// didn't find element
}

And even though you may have set the readonly attribute of the DOM object
to true, it may not actually make it readonly (though I expect it will in
most browsers that get that far).

Gérard
--
remove blah to email me

Mar 15 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Learning SQL Server | last post: by
5 posts views Thread by fred | last post: by
5 posts views Thread by John Smith | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.