Changing HTML input text style color from javascript

Discussion in 'Javascript' started by Perdit, Sep 21, 2003.

  1. Perdit

    Perdit Guest

    I am wondering why this does not work in javascript. The html color is
    set to automatic (none)

    element1, element2, etc are input fields (textboxes)

    document.myform.elements('element-'+counter).style='color: #FF0000'

    Thanks
     
    Perdit, Sep 21, 2003
    #1
    1. Advertising

  2. Perdit

    Evertjan. Guest

    Perdit wrote on 21 sep 2003 in comp.lang.javascript:

    > I am wondering why this does not work in javascript. The html color is
    > set to automatic (none)
    >
    > element1, element2, etc are input fields (textboxes)
    >
    > document.myform.elements('element-'+counter).style='color: #FF0000'


    document.myform(counter-1).style.color = '#FF0000'

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Sep 21, 2003
    #2
    1. Advertising

  3. Perdit

    Tom Guest

    "Evertjan." <> wrote in message
    news:Xns93FE12263EC3eejj99@194.109.133.29...
    > Perdit wrote on 21 sep 2003 in comp.lang.javascript:
    >
    > > I am wondering why this does not work in javascript. The html color is
    > > set to automatic (none)
    > >
    > > element1, element2, etc are input fields (textboxes)
    > >
    > > document.myform.elements('element-'+counter).style='color: #FF0000'

    >
    > document.myform(counter-1).style.color = '#FF0000'


    Let 's try another one:
    document.myform['element'+counter].style.color='#ff0000';

    Important things to note:
    - square brackets.
    - no dot or anything between "myform" and the opening bracket.
    - "myform" can be any string, as long as it is the same as
    the "name='myform' " bit in the actual form opening tag.
    - counter can be an integer 1, or a string "1", but not "01" or " 1" with a
    space.

    If you want to use a hyphen (-) like in your example line of code, the
    element names should also be renamed to element-1, element-2, etc. I think
    hyphens are legal in element names, but am not sure.
    HTH
    Tom
     
    Tom, Sep 22, 2003
    #3
  4. "Perdit" <> wrote in message
    news:...
    >I am wondering why this does not work in javascript. The html
    >color is set to automatic (none)
    >
    > element1, element2, etc are input fields (textboxes)
    >
    > document.myform.elements('element-'+counter).style='color: #FF0000'


    The elements collection of a form is an object so JavaScript property
    accessor syntax uses square brackets not parenthesises.

    <URL: http://jibbering.com/faq/#FAQ4_39 >

    The style property of an Element holds a reference to an object and
    assigning a CSS 'color' property to that object involves setting its
    color property:-

    document.forms['myform'].elements['element-'+

    counter].style.color = '#FF0000';

    Richard.
     
    Richard Cornford, Sep 22, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ashutosh
    Replies:
    3
    Views:
    9,052
    Andrew Thompson
    Jun 16, 2005
  2. Aaron Beall
    Replies:
    2
    Views:
    1,905
    Aaron Beall
    Sep 14, 2007
  3. Kamaljeet Saini
    Replies:
    0
    Views:
    475
    Kamaljeet Saini
    Feb 13, 2009
  4. Replies:
    7
    Views:
    409
    Mick White
    Feb 2, 2005
  5. Replies:
    10
    Views:
    243
    Randy Webb
    Jan 11, 2007
Loading...

Share This Page