object questions and difficulty

M

Michael Hill

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.forms[1].elements.length; i++ )
{
if ( document.forms[1].elements.name == myfield )
{
document.forms[1].elements.class = "newstylesheetformat";
}
}
}

Why is the document.forms[1].elements.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.forms[1].elements.length; i++ )
{
for ( j=0; j<document.forms[1].elements.properties.length; i++ )

{
alert(document.forms[1].elements.properties.name);
}
}
}
 
L

Lee

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

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

Lasse Reichstein Nielsen

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


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

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.forms[1].elements.attributes.length; i++ )

/L
 
M

Michael Hill

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.forms[1].elements.length; i++ )
{
if ( document.forms[1].elements.name == myfield )
{
for ( j=0; j<document.forms[1].elements.attributes.length; j++ )
{
if ( document.forms[1].elements.attributes[j].name == "class" )
{
document.forms[1].elements.attributes[j].value = "grey";
break;
}
}
}
}
Michael Hill said:
Why is the document.forms[1].elements.class property not accessible
using this method even though I specified it in the form?


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

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.forms[1].elements.attributes.length; i++ )

/L
 
L

Lasse Reichstein Nielsen

Michael Hill said:
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.forms[1].elements.length; i++ )
{
if ( document.forms[1].elements.name == myfield )
{
for ( j=0; j<document.forms[1].elements.attributes.length; j++ )
{
if ( document.forms[1].elements.attributes[j].name == "class" )
{
document.forms[1].elements.attributes[j].value = "grey";
break;
}
}
}
}


That shouldn't be necessary.

document.forms[1].elements.namedItem(myfield).className = "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.namedItem(myField);
elem.attributes.getNamedItem("class").value = "gray";

/L
 

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
474,102
Messages
2,570,645
Members
47,245
Latest member
ShannonEat

Latest Threads

Top