471,091 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,091 software developers and data experts.

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

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

{
alert(document.forms[1].elements[i].properties.name);
}
}
}
Jul 20 '05 #1
4 5402
Michael Hill <hi****@ram.lmtas.lmco.com> writes:
Why is the document.forms[1].elements[i].class property not accessible
using this method even though I specified it in the form?
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).
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[i].attributes.length; i++ )

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
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.

Jul 20 '05 #3
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[i].name == myfield )
{
for ( j=0; j<document.forms[1].elements[i].attributes.length; 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:
Michael Hill <hi****@ram.lmtas.lmco.com> writes:
Why is the document.forms[1].elements[i].class property not accessible
using this method even though I specified it in the form?


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

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


Jul 20 '05 #4
Michael Hill <hi****@ram.lmtas.lmco.com> writes:
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[i].name == myfield )
{
for ( j=0; j<document.forms[1].elements[i].attributes.length; j++ )
{
if ( document.forms[1].elements[i].attributes[j].name == "class" )
{
document.forms[1].elements[i].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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jmar | last post: by
3 posts views Thread by Kavindra Malik | last post: by
1 post views Thread by Jmar | last post: by
162 posts views Thread by techievasant | last post: by
275 posts views Thread by Astley Le Jasper | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.