readonly attribute

J

Jimbo

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("readonly", "true");
}

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

Many thanks,

Jimbo.
 
J

Justin McConnell

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.
 
D

David Dorward

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?
 
R

RobG

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("readonly", "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).
 
J

Justin McConnell

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?
 
G

Gérard Talbot

RobG wrote :
Jimbo said on 15/03/2006 6:25 AM AEST:

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-HTML/html.html#ID-88461592
In general it is safer to access DOM object properties directly rather
than use setAttribute.



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-HTML/html.html#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
 
G

Gérard Talbot

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/answer.py?answer=12348&topic=250


Gérard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top