-2

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.

Basically a user clicks a link then a js file is initiated. Immediately after a new HTML page loads.

I need this value passed to the HTML page in a form field.

Javascript:

var divElement = function(){
divCode = document.getElementById(div1).innerHTML;
return divCode; };

document.getElementById('adcode').value = divElement(); 

Afterwards it should be passed to this Form field

HTML Form Field:

<p>Ad Code:<br>
<input type="text" name="adcode" id="adcode"/>

  <br>
  </p>

Thanks for any help!

user2929293
  • 3
  • 1
  • 1
  • 2

2 Answers2

3

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>
Douglas Barbin
  • 3,595
  • 2
  • 14
  • 34
  • Hello Douglas, Your answer looks like it should work but I am pretty sure I know whats wrong now. It looks like the initial js file that holds the variable I need isnt getting used for the form on the html page. Its another js file thats being used for the form. So I need to pass the variable in the first js file to the other js file then use the code you gave me and it should work? – user2929293 Oct 30 '13 at 03:19
  • It looks like the current way its doing that is sending data into the URL and then pulling it from there... but the data I want is huge so I dont think thats possible. – user2929293 Oct 30 '13 at 03:44
1

Can it be this?:

function divElement(divCode){
return divCode; 
}

divElement(document.getElementById('adcode').value); 
Meowsome
  • 99
  • 1
  • 10