0

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?

6
  • Is your javascript even loaded? Commented Nov 18, 2013 at 6:27
  • @limelights - what exactly do you mean by loaded? Commented Nov 18, 2013 at 6:33
  • Can you call the function in Chrome dev tools for example? Commented Nov 18, 2013 at 6:48
  • yes, on inspecting element on Chrome browser, it is pointing to the .js file and the function is visible, however, it is not performing that function. I tried adding an alert window too, but no response. What could be the cause of this error? Commented Nov 18, 2013 at 6:52
  • In Chrome Dev-tools console can you run trial()? Commented Nov 18, 2013 at 7:32

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.