Anagha
Anagha

Reputation: 760

Calling javascript file in a django template

I want to call a javascript function in my html template and the following is my code:

project/app/static/js/my_javascript.js

function trial() 
{
    document.getElementById("demo").innerHTML = "Hi, I am your javascript from an external source";
}  

project/app/templates/app/my_html.html

{% load static files %} 
<!DOCTYPE html>
<html>
<body>
<script src = {% static "js/my_javascript.js" %}> </script>
<p id="demo">Original</p>
<button type = "button" onClick="trial()">Click me!</button>
</body>
</html>

However, my button is being displayed, but no function is being performed on click of the button. I checked 'inspect element' on my browser and the link to the javascript is fine, hence I'm unable to determine how to fix it.

What changes do you suggest?

Upvotes: 0

Views: 7745

Answers (1)

Anagha
Anagha

Reputation: 760

I had typed an extra tilde character at the end of the code (typo!). This was the cause of the error and it has been fixed. Thanks for the help.

Upvotes: 1

Related Questions