This has to be a simple question but I can't find the answer. I am trying to call my javascript function "Update_PointTracker_Account" within a modal so it executes when the modal is opened. For some reason it won't execute. However, the Onclick button calls the function perfectly. What am I doing wrong?
<div class="modal hide fade" id="Refresh_PointTracker_Program" data-keyboard="false" data-backdrop="static" aria-hidden="true">
<div class="modal-header" align='center'>
<h3>Refreshing PointTracker Program</h3>
</div>
<div class="modal-body" style="text-align: center">
<p> The points program is being refreshed</p>
</div>
<div style="text-align: center" id='updated_program_list'></div>
<script>
Update_PointTracker_Account();
</script>
<div class="modal-footer">
<button onclick ="Update_PointTracker_Account()" class="btn" aria-hidden="true">Update</button>
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
-
Where is that function at? Sounds like you are trying to execute it before it exists.MikeSmithDev– MikeSmithDev2013年03月05日 23:48:40 +00:00Commented Mar 5, 2013 at 23:48
-
The function 'Update_PointTracker_Account()' is located in my 'pointtracker.js" file. That file is included at the top of my html. So, isn't the function already defined? My onclick='Update_PointTracker_Account" works fine in the bottom of the modal. Why? thxfat fantasma– fat fantasma2013年03月06日 00:35:07 +00:00Commented Mar 6, 2013 at 0:35
4 Answers 4
That is because you have not defined the function Update_PointTracker_Account() at all.
It has to appear somewhere in the page, either as an inline JS or an imported JS file, and should look like:
function Update_PointTracker_Account() {
// Do something
}
2 Comments
I'm assuming this modal is being loaded with .innerHTML somewhere.
<script> tags do not run when inserted with innerHTML. Instead, you should call the function when you assign the innerHTML. The reason the button click works is because it's handled differently.
Comments
Try to use a callback when you call your modal or verify if Update_PointTracker_Account() is declared before the call.
Comments
first: unless You are in a html5 doc You should add type attribute to script tag, second: the function exists? hope it helps