Changing HTML input text style color from javascript

P

Perdit

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
 
E

Evertjan.

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'
 
T

Tom

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


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
 
R

Richard Cornford

Perdit said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top