0

I have a view dynamic variables that I would like to pass to an external javascript.

in the HTML I am setting a few hidden input fields, as such:

<input type=hidden id="varID" value="sourceID">

and then calling them in my external Javascript file as such:

varID = document.getElementById("varID").value;

When I check to see if there is anything in there, I always get an undefined. In my HTML, I am setting the hidden input field before I even al to load the external javascript file.

Any ideas of what I'm missing?

4

2 Answers 2

1

Try this in your external JavaScript.

$(document).ready(function() {    
    var varID = document.getElementById("varID").value;
});
Sign up to request clarification or add additional context in comments.

3 Comments

that did it... I don't know why... since everything else was loading properly without the document.ready.
$( document ).ready will make sure the Dom is ready before accessing any data in it. if it helps you please mark the answer.
I have to wait a minute before I can mark this correct... be patient.
0

Try this:

window.onload = function () {
   var varID = document.getElementById("varID").value;
}

Basically, the JavaScript code will only run once the page has loaded.

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.