Web Analytics Made Easy -
StatCounter referencing html form elements - CodingForum

Announcement

Collapse
No announcement yet.

referencing html form elements

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • referencing html form elements

    Hello there!


    The following code is intended to write the value of an html form element (a hidden input field, to be precise) into a javascript variable, and so far everything works perfectly.

    'test' is the variable that should hold the final value.

    'strFormName' holds the name of the html form (in string format).

    'DivMemoryText' is the name/id of the hidden field.


    Code:
    var test;
    		
    	function writeHidden(strFormName)
    	{
    	       document.forms(strFormName, 0).DivMemoryText.value = test;
    	}


    What I would like to do, is modify the function so that I can actually pass in the name of the 'hidden field' element (DivMemoryText), rather than having to use a static name every time.


    I've tried: -

    Code:
       function writeHidden(strFormName, objFieldName)
    	{
    	       document.forms(strFormName, 0).objFieldName.value = test;
    	}

    .......but nothiing seems to happen - literally (No errors; just nothing).


    If anybody has any ideas how I can better approach this problem, then I'd be very happy.


    Thanks.

  • #2
    function writeHidden(strFormName, objFieldName)
    {
    document.strFormName.objFieldName.value = test;
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

    Comment


    • #3
      Thanks, felgall!

      I couldn't get it to work exactly as you suggested, but I came up with a variation that seems to suit the purpose quite well. I think I probably failed to explain my situation in the necessary detail, but you definitely pointed me in the right direction in any case.


      The solution that I ended up with is as follows:


      Code:
      var test;
      
      function writeHidden(objFieldName)
      	{
      		objFieldName.value = test;
      					
      	}

      I call the function with:

      Code:
      writeHidden(document.Gateway.DivMemoryText);



      The whole purpose of the project in its entirety (in case you're interested) was to remember and reposition the scroll coordinates of a div within an .asp form after the page is submitted, in order to prevent the scrollable area from snapping back to its default position every time the user pressed the submit button.

      Everything seems to work just fine now in IE and Opera; I just need to find a way to make it compatible with Netscape and Firefox. Grrrr!

      At the moment, I'm retrieving and setting the scroll position using the following functions:


      Code:
      function setScroll(val)
      {
               test = val.scrollTop;
      }
      				
      				
      				
      function writeScroll(objDivName, strVBDictVar)
      {					
      	if (strVBDictVar != "" && strVBDictVar != "undefined")
      	{
      		objDivName.scrollTop = strVBDictVar;
      	}
      }


      Am I correct in assuming that I'll need to use something like:


      Code:
      posX += oLink.offsetLeft;
       posY += oLink.offsetTop;

      .......to set and retrieve coordinates under Netscape and Firefox?



      Thanks for your help, in any any case!

      Comment

      Working...
      X