help - can anyone tell me how to get this code snippet to work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawrencef@hlac.com.au

    help - can anyone tell me how to get this code snippet to work

    I’m new to JavaScript and am trying to creating a dynamic web form in
    a number of layers. From what I've read, to submit the entire form I
    need to have all the fields (that are in all the different layers on
    the page), present in the main form as hidden fields. I've done this
    and now and trying to write a function to loop through all the layers
    on the page and then all the elements within those layers and copy the
    field value to the hidden field of the same name in the main form on
    the page. Problem is I don’t know how to concatenate a variable into a
    code statement in JavaScript (assuming that’s how you term it). ie see
    the script below – in the last line of the script I'm trying to place
    2 variables into the statement ie document.MAINFO RM.elements.ELN AME.
    as it's currently written it causes error because mainform and elname
    are taken literally and there is no form named mainform and no element
    named elname so I'm assuming I need to amend this line so that the
    assigned values for these 2 variables are read instead of the literal
    values. Can anyone point me in right direction or can advise on
    correct way to do what I'm trying to do if I'm completely barking up
    the wrong tree.


    function populatemainfor m(mainform)
    {
    for (var f = 0; f < document.forms. length; f++) //loop thru all the
    forms in the document (except main one)
    {
    if (document.forms[f].name == mainform)
    {
    continue //skip loop if current form is the main form of the page
    }
    else
    {
    for (var e = 0; e < document.forms[f].elements.lengt h; e++) //loop
    thru all elements in the form.
    {
    var elname = document.forms[f].elements[e].name // assign current
    element name to elname variable
    document.mainfo rm.elements.eln ame.value =
    document.forms[f].elements[e].value;
    }
    }
    }
    }
    }
  • apatheticagnostic

    #2
    Re: help - can anyone tell me how to get this code snippet to work

    On May 7, 9:28 pm, lawren...@hlac. com.au wrote:
    I’m new to JavaScript and am trying to creating a dynamic web form in
    a number of layers. From what I've read, to submit the entire form I
    need to have all the fields (that are in all the different layers on
    the page), present in the main form as hidden fields. I've done this
    and now and trying to write a function to loop through all the layers
    on the page and then all the elements within those layers and copy the
    field value to the hidden field of the same name in the main form on
    the page. Problem is I don’t know how to concatenate a variable into a
    code statement in JavaScript (assuming that’s how you term it). ie see
    the script below – in the last line of the script I'm trying to place
    2 variables into the statement ie document.MAINFO RM.elements.ELN AME.
    as it's currently written it causes error because mainform and elname
    are taken literally and there is no form named mainform and no element
    named elname so I'm assuming I need to amend this line so that the
    assigned values for these 2 variables are read instead of the literal
    values. Can anyone point me in right direction or can advise on
    correct way to do what I'm trying to do if I'm completely barking up
    the wrong tree.
    >
    function populatemainfor m(mainform)
    {
            for (var f = 0; f < document.forms. length; f++) //loop thru all the
    forms in the document (except main one)
            {
                    if (document.forms[f].name == mainform)
                    {
                            continue  //skip loop ifcurrent form is the main form of the page
                    }
                    else
                    {
                            for (var e = 0; e < document.forms[f].elements.lengt h; e++) //loop
    thru all elements in the form.
                            {
                                    var elname= document.forms[f].elements[e].name // assign current
    element name to elname variable
                                    document.mainfo rm.elements.eln ame.value =
    document.forms[f].elements[e].value;
                                    }
                            }
                    }
            }
    >
    >
    >
    }

    Off the top of my head, try document.mainfo rm.elements[elname].value

    Comment

    • Tom de Neef

      #3
      Re: help - can anyone tell me how to get this code snippet to work

      <lawrencef@hlac .com.auschreef in bericht
      news:3a94039f-b22a-4755-9e12-557fb2cfb268@u3 6g2000prf.googl egroups.com...
      I’m new to JavaScript and am trying to creating a dynamic web form in
      a number of layers. From what I've read, to submit the entire form I
      need to have all the fields (that are in all the different layers on
      the page), present in the main form as hidden fields. I've done this
      and now and trying to write a function to loop through all the layers
      on the page and then all the elements within those layers and copy the
      field value to the hidden field of the same name in the main form on
      the page. Problem is I don’t know how to concatenate a variable into a
      code statement in JavaScript (assuming that’s how you term it). ie see
      the script below – in the last line of the script I'm trying to place
      2 variables into the statement ie document.MAINFO RM.elements.ELN AME.
      as it's currently written it causes error because mainform and elname
      are taken literally and there is no form named mainform and no element
      named elname so I'm assuming I need to amend this line so that the
      assigned values for these 2 variables are read instead of the literal
      values. Can anyone point me in right direction or can advise on
      correct way to do what I'm trying to do if I'm completely barking up
      the wrong tree.


      function populatemainfor m(mainform)
      {
      for (var f = 0; f < document.forms. length; f++) //loop thru all the
      forms in the document (except main one)
      {
      if (document.forms[f].name == mainform)
      {
      continue //skip loop if current form is the main form of the page
      }
      else
      {
      for (var e = 0; e < document.forms[f].elements.lengt h; e++) //loop
      thru all elements in the form.
      {
      var elname = document.forms[f].elements[e].name // assign current
      element name to elname variable
      document.mainfo rm.elements.eln ame.value =
      document.forms[f].elements[e].value;
      }
      }
      }
      }
      }

      1) Unless mainform is a variabale you have assigned the value "mainform"
      before, use
      if (document.forms[f].name == "mainform")
      2) elname is returned as a string. To use it in accessing a property with
      that name, use
      document.mainfo rm.elements[elname].value = ...

      Tom





      Comment

      • apatheticagnostic

        #4
        Re: help - can anyone tell me how to get this code snippet to work

        On May 8, 3:10 am, "Tom de Neef" <tden...@qolor. nlwrote:
        1) Unless mainform is a variabale you have assigned the value "mainform"
        before, use
        if (document.forms[f].name == "mainform")
        mainform is passed into the function

        Comment

        • Tom de Neef

          #5
          Re: help - can anyone tell me how to get this code snippet to work

          "apatheticagnos tic" <apatheticagnos tic@gmail.comsc hreef in bericht
          news:6e33ccaa-c601-4e72-9d6d-806a3571dc2d@59 g2000hsb.google groups.com...
          On May 8, 3:10 am, "Tom de Neef" <tden...@qolor. nlwrote:
          1) Unless mainform is a variabale you have assigned the value "mainform"
          before, use
          if (document.forms[f].name == "mainform")
          : mainform is passed into the function

          Yep!
          But then... it is likely passed as a DOM element, not as a string.
          Should´nt the conditional then be
          if (document.forms[f] == mainform)
          rather than
          if (document.forms[f].name == mainform)

          or
          if (document.forms[f].name == mainform.name)

          Tom


          Comment

          • apatheticagnostic

            #6
            Re: help - can anyone tell me how to get this code snippet to work

            On May 8, 6:24 am, "Tom de Neef" <tden...@qolor. nlwrote:
            "apatheticagnos tic" <apatheticagnos ...@gmail.comsc hreef in berichtnews:6e3 3ccaa-c601-4e72-9d6d-806a3571dc2d@59 g2000hsb.google groups.com...
            On May 8, 3:10 am, "Tom de Neef" <tden...@qolor. nlwrote:
            >
            1) Unless mainform is a variabale you have assigned the value "mainform"
            before, use
            if (document.forms[f].name == "mainform")
            >
            : mainform is passed into the function
            >
            Yep!
            But then... it is likely passed as a DOM element, not as a string.
            Should´nt the conditional then be
            if (document.forms[f] == mainform)
            rather than
            if (document.forms[f].name == mainform)
            >
            or
            if (document.forms[f].name == mainform.name)
            >
            Tom
            I assumed it was a string because of:

            document.forms[f].name == mainform

            and

            document.mainfo rm.elements.eln ame.value


            I don't have any idea how it actually is, but if it was passed as a
            dom element, these two lines would be different (I would assume)

            If it is a string, document[mainform].elements[elname].value may do
            the trick.

            Comment

            Working...