-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!

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.
1

Can it be this?:

function divElement(divCode){
return divCode; 
}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.