Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to reference an item from GetElementsByName?

Status
Not open for further replies.

gdrenfrew

Programmer
Joined
Aug 1, 2002
Messages
227
Location
GB
Hi,
I can't figure out how to reference individual items returned by getElementsByName . Can somebody help please?
This code only returns one item, but I cannot use the ID some must use the name, hence the use of getElementsByName rather than getElementByID

function()
{
var aInput = window.document.getElementsByName("Cell1")

alert(aInput.item()) etc just brings up null,
as does alert(aInput.item(0))

nb "Cell1" is the name attribute of a TD. I want to be able to reference that individual TD from my javascript, so I can change its colour.

Thanks
Graeme
 
If it only returns one item, it probably doesn't create the array....

alert(aInput) should return "[object]"

or else

alert(aInput.length) should return 1 meaning you have an array... Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
 
thanks, it was empty and i don't know why. i've worked around it by rewritng some bits of the page and just using getelementbyid. thanks
graeme
 
Checked the getElementByName with IE 6 and this functions only seems to pick up form elements.
Mozilla does OK.

<input type=text name=&quot;somename&quot;>
<input type=text name=&quot;somename&quot;>
<input type=text name=&quot;somename&quot;>
<input type=text name=&quot;uniquename&quot;>

<input type=button value=test onclick=test()>

<div name=&quot;mydiv&quot;>content of the div</div>
<script>
function test(){
var arrItems = document.getElementsByName('uniquename');
alert(arrItems.length);
alert(arrItems[0].name);

var arrItems = document.getElementsByName('somename');
alert(arrItems.length);
alert(arrItems[0].name);

var arrItems = document.getElementsByName('mydiv');
alert(arrItems.length);
alert(arrItems[0].name); // this does not work in mozilla
arrItems[0].innerHTML = &quot;this works OK in mozilla&quot;;
}
</script>
 
thanks for your help with this
great

graeme
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top