Accessing form elements dynamically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EvrimAgha
    New Member
    • Aug 2007
    • 11

    #1

    Accessing form elements dynamically

    Hi

    I am using an external JavaScript file that should handle number of similar forms and form elements on different html pages.

    What I need to do is , pass the name of the form, the two text boxes , name of a combobox and a checkbox group to the file.

    As an example , inside Webpage I have:

    Code:
    < body onload="javascript:runMyCode('myform1','textbox1','textbox2','checkgroup1','combo1')">
    and the external file has the following code:

    Code:
    function runMyCode(form,name,job,check,cbo) 
    {
    document.getElementById(form).getElementById(name).value='value1';
    document.getElementById(form).getElementById(job).value='value2';
    .....
    
    }

    But it is wrong and I am hoping JavaScript experts will help me solve this. Kindly tell me how to go about this.


    Thank you in advance
    Evrim
  • Dasty
    Recognized Expert New Member
    • Nov 2007
    • 101

    #2
    Function getElementById is method of document element. You can not call it on "form" DOM element. But, why dont you just call it that way?

    Code:
    function runMyCode(form,name,job,check,cbo)
    {
      document.getElementById(name).value='value1';
      document.getElementById(job).value='value2';
    }
    Two different elements can not have same id in valid page. But don't forget to set id properties to them. like:

    [HTML]<input type=text id=myid1 .......... >[/HTML]

    Comment

    • EvrimAgha
      New Member
      • Aug 2007
      • 11

      #3
      Originally posted by Dasty
      Function getElementById is method of document element. You can not call it on "form" DOM element. But, why dont you just call it that way?

      Code:
      function runMyCode(form,name,job,check,cbo)
      {
        document.getElementById(name).value='value1';
        document.getElementById(job).value='value2';
      }
      Two different elements can not have same id in valid page. But don't forget to set id properties to them. like:

      [HTML]<input type=text id=myid1 .......... >[/HTML]

      Thank you very much for your reply ....

      Could you please tell me how it is possible to access the form element itself?
      For example:

      Code:
      function goPlaces(theForm,theURL){
          document.theForm.action=theURL;
          document.theForm.submit();
      }
      Generates errors ...........


      Thanks again
      Evrim

      Comment

      • Dasty
        Recognized Expert New Member
        • Nov 2007
        • 101

        #4
        Code:
        function goPlaces(theForm,theURL)
        {
          document.theForm.action=theURL;
          document.theForm.submit();
        }
        You are sending theForm attribute as string. And you can not use string that way. But you was close. Use this instead:

        Code:
        function goPlaces(theForm,theURL)
        {
          document[theForm].action=theURL;
          document[theForm].submit();
        }
        Dont forget that in this case "theForm" string is name (not id) of the form element.

        But you can access it by id too:
        Code:
        document.getElementById('id_of_form_element').action
        And you have 'forms' array in document element as well:
        Code:
        document.forms[0].action
        Choose whatever you want. (but I prefer referencing any elements by id)

        Comment

        • EvrimAgha
          New Member
          • Aug 2007
          • 11

          #5
          Originally posted by Dasty
          You are sending theForm attribute as string. And you can not use string that way. But you was close. Use this instead:

          Code:
          function goPlaces(theForm,theURL)
          {
            document[theForm].action=theURL;
            document[theForm].submit();
          }
          Dont forget that in this case "theForm" string is name (not id) of the form element.

          But you can access it by id too:
          Code:
          document.getElementById('id_of_form_element').action
          And you have 'forms' array in document element as well:
          Code:
          document.forms[0].action
          Choose whatever you want. (but I prefer referencing any elements by id)

          Thank you for helping me :-)

          Comment

          Working...