SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Bath, UK.
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Referring to Javascript objects using variables.

    Is it possible to construct a from object as a string and then use it. Currently i am looping through all the form objects and testing to see if the .name is equal to a string: e.g. code:

    function pass_enrollment_id (enrollment_id) {

    if (document.forms[0].elements[i].type == "text") {
    if (document.forms[0].elements[i].name == "r" + enrollment_id + "_attended") {
    document.forms.result_maint.h_attended.value = document.forms[0].elements[i].value;
    }
    }

    I want to avoid looping through all the objects and refer to the object directly.

    Any ideas???

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Perhaps this might do the trick?

    Code:
    function pass_enrollment_id(enrollment_id)
    {
      document.forms['result_maint'].elements['h_attended'].value
        = document.forms[0].elements['r' + enrollment_id + '_attended'].value;
    }
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44░ 56.537' W 123░ 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a reason you aren't using document.getElementsByName()?

  4. #4
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Bath, UK.
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Erik,

    I havn't used Javascript for over three years now and am still a bit rusty, my code is now streamlined.


Bookmarks

Posting Permissions

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