Given my html file, the console gives an error code:
Reference Error: loadData not defined.
Based from other answers I've rechecked mine where,
- My function is declared just before the
</body>, - I included the javascript library at the
<head></head>; - and even changed my
<input type= submitto<input type= button - and if it helped, I tried deleting my form tags where it is enclosed (but this is for another question regarding query strings).
Here's how it would look like now:
<head>
<script src = "//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<body>
{%for document in documents %}
<li> {{document.filename}}
<input type="button" id="{{ document.id }}" onclick="loadData(this.id)" name = "load-data" value="Add to Layer"/>
</li>
{%endfor%}
<script type ="text/javascript">
function loadData(documentId){
$.ajax({
url:"/load",
data: {'documentId': documentId},
type: 'GET',
success: function(){
window.location.href = "http://127.0.0.1:8000/url/locations";
}
});
}
</script>
</body>
I can't seem to find an explanation why the function won't fire.
<script>tag doesn't close your javascript function."in the button'snameattribute. Your point 1 shouldn't matter because regardless of where the script is it will have been run by the time the user actually clicks on the buttons. Points 2-4 shouldn't matter at all as far as whether the function can be called from theonclick.