How to pass Variable from External JavaScript to HTML Form

后端 未结 2 631
北荒
北荒 2021-01-29 13:46

I have been trying to pass a value from an external javascript file to an HTML form with no luck. The files are rather large so I am not sure I can explain it all but ill try.

相关标签:
2条回答
  • 2021-01-29 14:19

    Can it be this?:

    function divElement(divCode){
    return divCode; 
    }
    
    divElement(document.getElementById('adcode').value); 
    
    0 讨论(0)
  • 2021-01-29 14:24

    Your HTML file needs to reference the JavaScript js file. Have a function in your JavaScript that returns the value that you need. Use JavaScript (I like jQuery) to set the form field to what you need.

    JS file:

    <script>
      var divElement = function(){
      divCode = document.getElementById(div1).innerHTML;
      return divCode; };
    
      document.getElementById('adcode').value = divElement(); 
    
      function GetDivElement() {
        return divElement();
      }
    </script>
    

    HTML file:

    <p>Ad Code:
      <br />
      <input type="text" name="adcode" id="adcode"/>
      <br />
    </p>
    
    <script src="wherever that js file is" />
    <script>
      window.onload = function() {
          document.getElementById('adcode').value = GetDivElement();
      }
    </script>
    

    Although, really, this might do what you want (depending on what you are trying to do):

    <p>Ad Code:
      <br />
      <input type="text" name="adcode" id="adcode"/>
      <br />
    </p>
    
    <script src="wherever that js file is" />
    <script>
      window.onload = function() {
          GetDivElement();
      }
    </script>
    
    0 讨论(0)
提交回复
热议问题