object questions and difficulty

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Hill

    object questions and difficulty

    This a valid call:
    document.myform .elements['myelement']
    so is this:
    document.forms[0].elements[1]
    but not this:
    document.forms[0].elements['myelement']

    So, I have to iterate through the elements to find my field and make a
    change to the element properties

    myfield = "myelement" ;
    for ( i=0; i<document.form s[1].elements.lengt h; i++ )
    {
    if ( document.forms[1].elements[i].name == myfield )
    {
    document.forms[1].elements[i].class = "newstylesheetf ormat";
    }
    }
    }

    Why is the document.forms[1].elements[i].class property not accessible
    using this method even though I specified it in the form?

    If I wanted to iterate through the properties of the elements how would
    I do this, like this?

    for ( i=0; i<document.form s[1].elements.lengt h; i++ )
    {
    for ( j=0; j<document.form s[1].elements[i].properties.len gth; i++ )

    {
    alert(document. forms[1].elements[i].properties.nam e);
    }
    }
    }


  • Lasse Reichstein Nielsen

    #2
    Re: object questions and difficulty

    Michael Hill <hillmw@ram.lmt as.lmco.com> writes:
    [color=blue]
    > Why is the document.forms[1].elements[i].class property not accessible
    > using this method even though I specified it in the form?[/color]

    Probably because it should be
    document.forms[1].elements[i].className
    The renaming is most likely because "class" is a restricted word or keyword
    in many languages (including Javascript).
    [color=blue]
    > If I wanted to iterate through the properties of the elements how would
    > I do this, like this?[/color]

    What do you mean by "properties "?

    In Javascript, an object has properties. You iterate through (some of) them
    with
    for (var propName in objRef) { ... propName ... }

    If you mean the attributes of the html tag, it ".attributs " that you need
    to run through:
    for ( j=0; j<document.form s[1].elements[i].attributes.len gth; i++ )

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Lee

      #3
      Re: object questions and difficulty

      Michael Hill said:[color=blue]
      >
      >This a valid call:
      > document.myform .elements['myelement']
      >so is this:
      > document.forms[0].elements[1]
      >but not this:
      > document.forms[0].elements['myelement'][/color]

      In what browser is that not valid? It works in Netscape and IE.

      Comment

      • Michael Hill

        #4
        Re: object questions and difficulty

        Lasse,

        Thanks for helping. So you are saying everytime I want to change the class of
        my input text item I have to do this?

        for ( i=0; i<document.form s[1].elements.lengt h; i++ )
        {
        if ( document.forms[1].elements[i].name == myfield )
        {
        for ( j=0; j<document.form s[1].elements[i].attributes.len gth; j++ )
        {
        if ( document.forms[1].elements[i].attributes[j].name == "class" )
        {
        document.forms[1].elements[i].attributes[j].value = "grey";
        break;
        }
        }
        }
        }

        Lasse Reichstein Nielsen wrote:
        [color=blue]
        > Michael Hill <hillmw@ram.lmt as.lmco.com> writes:
        >[color=green]
        > > Why is the document.forms[1].elements[i].class property not accessible
        > > using this method even though I specified it in the form?[/color]
        >
        > Probably because it should be
        > document.forms[1].elements[i].className
        > The renaming is most likely because "class" is a restricted word or keyword
        > in many languages (including Javascript).
        >[color=green]
        > > If I wanted to iterate through the properties of the elements how would
        > > I do this, like this?[/color]
        >
        > What do you mean by "properties "?
        >
        > In Javascript, an object has properties. You iterate through (some of) them
        > with
        > for (var propName in objRef) { ... propName ... }
        >
        > If you mean the attributes of the html tag, it ".attributs " that you need
        > to run through:
        > for ( j=0; j<document.form s[1].elements[i].attributes.len gth; i++ )
        >
        > /L
        > --
        > Lasse Reichstein Nielsen - lrn@hotpop.com
        > Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        > 'Faith without judgement merely degrades the spirit divine.'[/color]

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: object questions and difficulty

          Michael Hill <hillmw@ram.lmt as.lmco.com> writes:
          [color=blue]
          > So you are saying everytime I want to change the class of my input
          > text item I have to do this?
          >
          > for ( i=0; i<document.form s[1].elements.lengt h; i++ )
          > {
          > if ( document.forms[1].elements[i].name == myfield )
          > {
          > for ( j=0; j<document.form s[1].elements[i].attributes.len gth; j++ )
          > {
          > if ( document.forms[1].elements[i].attributes[j].name == "class" )
          > {
          > document.forms[1].elements[i].attributes[j].value = "grey";
          > break;
          > }
          > }
          > }
          > }[/color]

          That shouldn't be necessary.

          document.forms[1].elements.named Item(myfield).c lassName = "gray";

          If you insist on changing the HTML attribute value (some browsers don't
          link className and the class Attribute element):

          var elem = document.forms[1].elements.named Item(myField);
          elem.attributes .getNamedItem(" class").value = "gray";

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          Working...