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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference element with tricky name 1

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
Hi there,

I have a series of "select" (drop-down) HTML form elements which have their name attribute set to "dateCollected[d]", "dateCollected[M]" and "dateCollected[Y]" respectively. I'm struggling to access them via JS. I've tried a number of techniques.

What would people suggest?

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Sorted. I just found the elements array of the form so was able to access the elements via an index number e.g.
Code:
document.forms[0].elements[0].selectedIndex = 0;

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
better way, so you can more easily determine which element you're referencing, is something like this:

Code:
document.forms['YourFormName'].elements['dateCollected[Y]'].selectedIndex = 0;



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Fantastic, cheers cLFlaVA! It'll be much easier to read when I come back to the code in a few months time. I hadn't realised you could reference elements like that.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
no prob.

in my opinion, that is the overall preferred method, better than the index reference:

[tt]document.forms[0].elements[3][/tt]

and better than the implied reference:

[tt]document.myFormName.myElementName;[/tt]

as it gives you the most visibility into what's going on in your code.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I only ever have a chance to dabble with Javascript as and when I need it - so don't often get a chance to learn how to do things properly!

Many thanks for the pointer.

By the way, I like the Google Maps Easter Eggs posted on your site - very cool!

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top