chadmichael Posted February 2, 2006 Share Posted February 2, 2006 Hello.I've ran into a strange, and perhaps interesting, syntax deadend. I'm trying to access a series of from elements from a javascript function that "enables" them -- sets their disabled value to false. Something like the following . . . form.firstName.disabled=false;form.lastName.disabled=false;form.favoriteColor[0]=false;form.favoriteColor[1]=false;form.favoriteColor[2]=false; NOTE: the array looking notation is the way to name a single element of a radio or check box set. In this case there are three radio boxes with the name "favoriteColor". Perhaps their values are red, blue, and green. Now, my problem. I'm working with some form field names that are tied to back end stuff that, at the least would make it very hard for me to change the names. These include a series of text input fields with the names "arrayPet[0]" "arrayPet[1]" "arrayPet[2]"These are in the html such as <input type="text" name="arrayPet[0]"/>The purpose of these strange names is to allow the user to enter names for one to three pets that are received in th back end under a single array. But that's beside the point. The problem with these names comes when you try to reference these text elements in the function described above. I end up with : form.arrayPet[0].disabled=false; form.arrayPet[1].disabled=false; form.arrayPet[2].disabled=false; And javascript is confused. I imagine its because the syntax is not distinquishable from what it would be if there were a radio or checkbox set named arrayPet.Anyone know how to reference these text inputs invidually without running into this sytax conflict? It doesn't work to reference them as members of the form.elements[] collection because, due to the dynamic nature of the page, its impossible to know the index of these elements.ThanksChad Link to comment Share on other sites More sharing options...
aspnetguy Posted February 2, 2006 Share Posted February 2, 2006 try referenceing them like this document.getElementById('arrayPet[0]').disabled = false; you will have to change the input boxes like this to add an id attribute <input type="text" name="arrayPet[0]" id="arrayPet[0]"/> Link to comment Share on other sites More sharing options...
chadmichael Posted February 2, 2006 Author Share Posted February 2, 2006 Thanks man!So, is that the reason for ID -- to identify each element individually becuase names don't necessarily have to be unique? Link to comment Share on other sites More sharing options...
chadmichael Posted February 2, 2006 Author Share Posted February 2, 2006 I saw this syntax also ( in a book ) document.forms["logonForm"].elements["username"]seems like this will probably work. Link to comment Share on other sites More sharing options...
aspnetguy Posted February 2, 2006 Share Posted February 2, 2006 yes...ids must be unique!take your pick if they both work although be sure to check them in FireFox too...FF prefers getElementById(). Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now