1

I have a function that gets called during a control update where 'This' is passed so the function knows what control is updating. I am also calling the function at page load to get initial values but sending document.getElementById() doesn't seem to work. What should I be passing here?

For example:

    <script type="text/javascript">

    window.onload = function () {
        alert("Got to window.onload");
        materialSelectionChange(document.getElementById('SquaresDropDownList'));

    };


</script>

in the js file...

function materialSelectionChange(obj) {

alert("Got into function");
}

Also, it does fire the js function on change properly

EDIT

The problem seems to be the load time of the JS document. The function wasn't successfully being called at that point because apparently the JS file hadn't finished loading. Likely because of window.onload. It works if I move the function into the page rather than in the JS file. Is there a way I can add delay so I know the page and it's components are fully loaded?

1
  • I am not sure why it is not working for you, but this works for me.. Try this.. jsfiddle.net/cv3saxd6 Commented Jan 16, 2015 at 2:57

3 Answers 3

4

You are not delegating for window load event, you are invoking it, also your missing quotes around the id:

window.onload = myFunction(document.getElementById(typeSelect));

Try wrapping it around:

window.onload = function() {
     myFunction(document.getElementById('typeSelect')); //id in quotes
};

EDIT

You must take care of js file import, import must be first before invoking the function within:

<script src='your-script-file.js'></script>

<script type="text/javascript">
    window.onload = function () {
        materialSelectionChange(document.getElementById('SquaresDropDownList'));
    };
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

That didn't seem to do it either. Does it matter that I used " rather than ' in my document.get...? Also, in this situation, I am calling this function about 30 times for different selects, My alert fires at the top of the list for the selects, but does not fire within the function I want to call.
0
<select id="typeSelect" onchange="myFunction(this)">

window.onload = function(){
  myFunction.bind(document.getElementById('typeSelect'));
}

3 Comments

<script type="text/javascript"> window.onload = function () { alert("Got to window.onload"); materialSelectionChange(document.getElementById('SquaresDropDownList')); }; </script> ... function materialSelectionChange(obj) { alert("Got into function"); }
Edit your question and paste it there.
Some explanation would be appreciated.
0

The problem seems to be the load time of the JS document. The function wasn't successfully being called at that point because apparently the JS file hadn't finished loading. It works if I move the function into the page rather than in the JS file. Is there a way I can add delay so I know the page and it's components are fully 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.