470,810 Members | 873 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Set a properti of an array of objects

Hello my name is Alejandro and i've a question.
I need to make a functions with this characteristics:
- params: an array of strings with the names of components in a page
(for example, some inputs)
- body: i need to assign to the property readOnly the value true for
all the objects in the array

Anybody know how can i do this?

Thanks,

Alejandro
Jul 20 '05 #1
1 1284
a_********@hotmail.com (Alejandro Narancio) writes:
I need to make a functions with this characteristics:
- params: an array of strings with the names of components in a page
(for example, some inputs)
- body: i need to assign to the property readOnly the value true for
all the objects in the array

Anybody know how can i do this?


There is a problem with this. *Names* of input controls are only
meaningfull in the scope of the form they are in. Two different forms
can have controls with the same names. That means that a *name* is not
necessarily unique for elements in a page. The id attribute should
give unique identifiers, but you don't always use those on input elements.

With that in mind, here is a function:
---
function setReadOnly(names,scope) {
scope = scope || document; // default to document
var hasGEBN = Boolean(scope.getElementsByName);
for (var i=0;i<names.length;i++) {
var elems = hasGEBN?scope.getElementsByName(names[i]):
scope.all[names[i]];
for (var j=0;j<elems.length;j++) {
elems[j].readOnly = true;
}
}
}
---
You can call it as
---
setReadOnly(["foo","bar","baz"]);
---
or, if you know which form the controls are in,
---
setReadOnly(["input1","input2","input3"],document.forms['myForm']);
---

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by its me | last post: by
2 posts views Thread by James | last post: by
7 posts views Thread by Robert Mark Bram | last post: by
21 posts views Thread by yeti349 | last post: by
7 posts views Thread by heddy | last post: by
2 posts views Thread by JJA | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.