Murphy1976
Murphy1976

Reputation: 1475

Passing Variables to External Javascript

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?

Upvotes: 0

Views: 82

Answers (2)

tech-gayan
tech-gayan

Reputation: 1413

Try this in your external JavaScript.

$(document).ready(function() {    
    var varID = document.getElementById("varID").value;
});

Upvotes: 1

rxjmx
rxjmx

Reputation: 2181

Try this:

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

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

Upvotes: 0

Related Questions