variable names in javascript

Results 1 to 2 of 2

Thread: variable names in javascript

  1. #1
    Join Date
    Dec 1969

    Default variable names in javascript

    Hi,<BR><BR>I have to generate variable names dynamically in javascript. How can I do that. The situation is like this. I have total 10 items in my page and everything is hidden. Depends on the user selection from dropdown box, I have to display that many no. of items in my page. I am doing this using DHTML. That&#039;s working fine. Now, I need to write javascript code to validate the data entered into these fields. The filed names are like this.<BR>T1, T2, T3, ...... T10. I need validate the field value using<BR>if (Request_Form.T1.value.length &#060; 1)<BR>if (Request_Form.T2.value.length &#060; 1), ......<BR><BR>And like this I have 10 different items. So, instead of writting each and everything , I am trying to put everything is a loop if I can generate the variable name dynamically. How can I do that. Thanks for you help..JY

  2. #2
    Join Date
    Dec 1969

    Default Those are NOT variables...

    Those are elements of a form, and so the fit the DOM (Document Object Model).<BR><BR>SO:<BR><BR>for ( fnum = 1; fnum &#060;= 10; ++fnum )<BR>{<BR>&nbsp; &nbsp; if ( document.Request_Form.elements["T"+fnum].value.length &#060; 1 ) ...<BR>&nbsp; &nbsp; ...<BR>}<BR><BR>Learn how the DOM works. Top level is the "window" object. It has collections of "forms" and "images" and and and. And then each form has a collection of elements. Some elements have sub-collections (e.g., if you have more than one element of the same name). Etc.<BR><BR>Unfortunately, the DOM for NS 4.x and NS 6.x and MSIE all differ somewhat, but if you stick with the older forms (of which document.FormName.elements[...] is one), they all work the same.<BR><BR>p.s.: If you insist on doing it your way, read up on the "eval( )" function in JS. Fair warning, you&#039;ll be taking a big performance hit. But what the heck...this is in the browser, anyway.<BR><BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts