473,626 Members | 3,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.formNa me.inputTagName .setAttribute(" readonly", "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
7 5492
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.getEle mentById instead of document.formNa me.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.getEle mentById instead of document.formNa me.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.formNa me.inputTagName .setAttribute(" readonly", "true");
setAttribute has not been consistently implemented, use:
document.formNa me.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.formNa me.inputTagName .setAttribute(" readonly", "true");
setAttribute has not been consistently implemented, use:
document.formNa me.inputTagName .readonly = "true";


It should be:

document.formNa me.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.formNa me.inputTagName .setAttribute(" readonly", "true");


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


It should be:

document.formNa me.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7214
by: Fred K. | last post by:
Hi, I have form text fields that have the attribute readonly. I wish to use a javascript (invoked by a button on my page) to erase the values and set the readonly attribute to false. How do I do this? Here's what I have that is not working with respect to setting readonly to false: function nsfields ( bDefaults ) {
3
25312
by: Matt | last post by:
I want to know if readOnly attribute doesn't work for drop down list? If I try disabled attribute, it works fine for drop down list. When I try text box, it works fine for both disabled and readOnly attribute. For example, #1 will work, but #2 doesn't work 1) <SELECT name="streetDirection" class="FormInput" DISABLED> In JavaScript, I have InputForm.streetDirection.disabled = false; 2) <SELECT name="streetDirection" class="FormInput"...
2
4427
by: upersson | last post by:
Hi! When accessing my web page I build up the javascript and html dynamically. Depending on an parameter I want to set all the fields (input tags) readonly. How do I do this? Of course I can write if statements to add different input tags with and without the readonly attribute. But is it possible to write a javascript that onload search the page and add readonly to all input tags? Regards /Ulrika
2
1862
by: Learning SQL Server | last post by:
Forgive the repost, but this is really troublesome and I hope SOMEONE can shed some light on it. I am trying to update a datarow via in-place editing from a dataset stored in session. When I try to write the new value into the selected row, I always get the following error : Column is read-only. in my UpdateCommand event I have:
5
10298
by: fred | last post by:
With a Class is there any difference between a readonly property and function besides having to use Get/End Get. Are there any performance/resource advantages for either. Thanks Fred
6
10629
by: Hako | last post by:
Hello All, I have a function to set readonly or editable of a textctrl. I'd like to make the textctrl initial set readonly and use other event funciton to set editable of the textctrl but it always can editable. How to set a textctrl can editable or readonly? Any Ideas? (see short snippet below) Thanks.
5
2184
by: John Smith | last post by:
Is it possible to set DataGrid's ReadOnly property to True/False at runtime? An example: <asp:datagrid id="dg1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundColumn DataField="id" ReadOnly="True" HeaderText="ID"></asp:BoundColumn> </Columns> <asp:datagrid>
1
2428
by: Raja | last post by:
Hi Everybody Just playing with ObjectDataSource and noticed the following. I have a Gridview which binds to a ObjectDataSource. ObjectDataSource gets data from a typed dataset created with VWD. In the table from which the data is coming has a Primary Key field. Gridview is set to edit mode. DataKeyNames = "PK_Field". Now if PK field is not readonly, then data is updated successfully. But if I set PK Field to readonly, then data is not...
3
1849
by: Hamed | last post by:
Hello I have a DataGrid object in my ASP.NET page that has the following template column. When I put the "readonly" attribute in the INPUT tag, it generates readonly="". <asp:TemplateColumn HeaderText="Kg Price" HeaderStyle-Width="5%"> <ItemTemplate><INPUT class="ep" id=kf readOnly value='<%# DataBinder.Eval(Container, "DataItem.KgFee") %>' name=kf runat="server"></ItemTemplate>
0
8269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8203
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
7203
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1515
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.