Set a properti of an array of objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alejandro Narancio

    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
  • Lasse Reichstein Nielsen

    #2
    Re: Set a properti of an array of objects

    a_narancio@hotm ail.com (Alejandro Narancio) writes:
    [color=blue]
    > 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?[/color]

    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(nam es,scope) {
    scope = scope || document; // default to document
    var hasGEBN = Boolean(scope.g etElementsByNam e);
    for (var i=0;i<names.len gth;i++) {
    var elems = hasGEBN?scope.g etElementsByNam e(names[i]):
    scope.all[names[i]];
    for (var j=0;j<elems.len gth;j++) {
    elems[j].readOnly = true;
    }
    }
    }
    ---
    You can call it as
    ---
    setReadOnly(["foo","bar","ba z"]);
    ---
    or, if you know which form the controls are in,
    ---
    setReadOnly(["input1","input 2","input3"],document.forms['myForm']);
    ---

    Good luck
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    Working...