Getting form details with a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paul@paullee.com

    Getting form details with a function

    Hi all,
    Is there a way to access a forms details by sending its parameters to a
    function. For example, I have a form
    named "myForm" populated by a drop down box, so that <select
    name="mySel">

    I am trying to send the form and drop down box name to a javascript
    function, eg onChange="updat e( "myForm", "mySel")
    so that, within the Javascript update(formName , selName) function
    I can access the forms doing something like this:

    update(formName , selName)
    {
    selected_index = document.forms. formName.select edIndex;
    selected_value = document.forms. formName.selNam e[selectedIndex];
    }

    so that I can access the specified value using the input arguments to
    the function, rather than doing

    selected_index = document.forms. myForm.selected Index;
    selected_value = document.forms. myFormn.mySel[selectedIndex];

    The reason for this is because I have a lot of drop box boxes that are
    changed by the user.

    Thanks in advance

    Paul
    --


  • Jarmo

    #2
    Re: Getting form details with a function

    <paul@paullee.c om> wrote in message
    news:1109173933 .187775.14550@l 41g2000cwc.goog legroups.com...[color=blue]
    > Hi all,
    > Is there a way to access a forms details by sending its parameters to a
    > function. For example, I have a form
    > named "myForm" populated by a drop down box, so that <select
    > name="mySel">
    >
    > I am trying to send the form and drop down box name to a javascript
    > function, eg onChange="updat e( "myForm", "mySel")
    > so that, within the Javascript update(formName , selName) function
    > I can access the forms doing something like this:
    >
    > update(formName , selName)
    > {
    > selected_index = document.forms. formName.select edIndex;
    > selected_value = document.forms. formName.selNam e[selectedIndex];
    > }[/color]

    Some variant of the following would work.

    <select name=fred onchange="docha nge(this)">
    <option value="bob">Bob
    <option value="jim">Jim
    </select>

    function dochange(elt)
    {
    alert(elt.value );
    }


    Comment

    • Randy Webb

      #3
      Re: Getting form details with a function

      paul@paullee.co m wrote:
      [color=blue]
      > Hi all,
      > Is there a way to access a forms details by sending its parameters to a
      > function. For example, I have a form
      > named "myForm" populated by a drop down box, so that <select
      > name="mySel">
      >
      > I am trying to send the form and drop down box name to a javascript
      > function, eg onChange="updat e( "myForm", "mySel")
      > so that, within the Javascript update(formName , selName) function
      > I can access the forms doing something like this:
      >
      > update(formName , selName)
      > {
      > selected_index = document.forms. formName.select edIndex;
      > selected_value = document.forms. formName.selNam e[selectedIndex];
      > }
      >
      > so that I can access the specified value using the input arguments to
      > the function, rather than doing
      >
      > selected_index = document.forms. myForm.selected Index;[/color]

      That one won't give you what you are after unless the form is named
      forms and the select is named myForm
      [color=blue]
      > selected_value = document.forms. myFormn.mySel[selectedIndex];
      > The reason for this is because I have a lot of drop box boxes that are
      > changed by the user.[/color]

      You start with this groups FAQ, and some of its notes.



      function update(formName , selName){
      var frm = document.forms[formName];
      var selectElement = frm.elements[selName];
      var selectIndex = selectElement.o ptions[selectElement.s electedIndex]
      var selectValue = selectIndex.val ue;
      }

      --
      Randy
      comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

      Comment

      • paul@paullee.com

        #4
        Re: Getting form details with a function

        Hi,
        I read the FAQ and it answered some questions, but I am still having
        some problems.

        I've tried your suggestion, but I still get problems. My code is as
        follows
        (just for one drop-down box at the moment)

        <script type="text/javascript">
        function updateStuff(for mName,selName)
        {
        var frm = document.forms[formName];
        var selectElement = frm.elements[selName]; <!-- error here -->
        var selectIndex = selectElement.o ptions[selectElement.s electedIndex];
        var selectValue = selectIndex.val ue;

        alert(selectVal ue);

        }
        </script>


        <form action="ViewAll ocation.php" name="myForm">
        <select name="mySel" onChange="updat eStuff(myForm,m ySel)">
        <option value="abc">abc </option>
        <option value="12">12</option>
        <option value="18">18</option>
        </select>
        </form>

        </BODY>
        </HTML>

        Basically, it gets to the indicated line and then I get an error
        message saying " 'elements' is null or not an object "

        TIA

        Paul

        Comment

        • Evertjan.

          #5
          Re: Getting form details with a function

          wrote on 24 feb 2005 in comp.lang.javas cript:[color=blue]
          > I've tried your suggestion[/color]

          This is NOT email, but Usenet.

          Please quote where you're responding on, or
          email your response privatly to the one you are calling "you".


          --
          Evertjan.
          The Netherlands.
          (Replace all crosses with dots in my emailaddress)

          Comment

          • Mick White

            #6
            Re: Getting form details with a function

            paul@paullee.co m wrote:[color=blue]
            > Hi,
            > I read the FAQ and it answered some questions, but I am still having
            > some problems.
            >
            > I've tried your suggestion, but I still get problems. My code is as
            > follows
            > (just for one drop-down box at the moment)
            >
            > <script type="text/javascript">
            > function updateStuff(for mName,selName)
            > {
            > var frm = document.forms[formName];
            > var selectElement = frm.elements[selName]; <!-- error here -->
            > var selectIndex = selectElement.o ptions[selectElement.s electedIndex];
            > var selectValue = selectIndex.val ue;
            >
            > alert(selectVal ue);
            >
            > }
            > </script>
            >
            >
            > <form action="ViewAll ocation.php" name="myForm">
            > <select name="mySel" onChange="updat eStuff(myForm,m ySel)">[/color]

            You need to pass form name and select name to the updateStuff function,
            which are strings.

            onChange="updat eStuff('myForm' ,'mySel')"

            I don't see any value in what you're doing, though.

            [snip]

            Mick

            Comment

            • Randy Webb

              #7
              Re: Getting form details with a function

              paul@paullee.co m wrote:[color=blue]
              > Hi,
              > I read the FAQ and it answered some questions, but I am still having
              > some problems.[/color]

              You missed part of it. It talks of quoting what you are replying to so
              that the people who read it know what you are referring to.
              [color=blue]
              > I've tried your suggestion, but I still get problems. My code is as
              > follows
              > (just for one drop-down box at the moment)
              >
              > <script type="text/javascript">
              > function updateStuff(for mName,selName)
              > {
              > var frm = document.forms[formName];
              > var selectElement = frm.elements[selName]; <!-- error here -->
              > var selectIndex = selectElement.o ptions[selectElement.s electedIndex];
              > var selectValue = selectIndex.val ue;
              >
              > alert(selectVal ue);
              >
              > }
              > </script>
              >
              >
              > <form action="ViewAll ocation.php" name="myForm">
              > <select name="mySel" onChange="updat eStuff(myForm,m ySel)">[/color]

              onChange="updat eStuff('myForm' ,'mySel')"

              or:

              onChange="updat eStuff(this.for m,this)"
              I prefer the second but it changes your function a little bit. With the
              above, it would become something like this:

              function updateStuff(for mRef,selRef){
              //No need to define frm, you have a reference
              //to it already in formRef. Same goes for selectIndex
              //It is passed as selRef. What you end up with is this:

              var selectValue = selName.value;
              }
              [color=blue]
              > <option value="abc">abc </option>
              > <option value="12">12</option>
              > <option value="18">18</option>
              > </select>
              > </form>
              >
              > </BODY>
              > </HTML>
              >
              > Basically, it gets to the indicated line and then I get an error
              > message saying " 'elements' is null or not an object "[/color]

              See Above.


              --
              Randy
              comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
              Answer:It destroys the order of the conversation
              Question: Why?
              Answer: Top-Posting.
              Question: Whats the most annoying thing on Usenet?

              Comment

              Working...