0

I'm new to programming. I'm trying to call a function in html, but it isn't working. What's the problem? Thanks in advance. (Don't ask about the whole integration thing).

My HTML code:

<!DOCTYPE html>
<html>
    <head>
    <script src="IntegrationCalculator.js"></script>
    <script>
        integrationCalculator(7);
    </script>


</head>
<body>
    <p id="tester">
        Didn't work :(
    </p>
</body>

My javascript code:

var integrationCalculator = function (variable1) {
if (variable1 = 7) {
    document.getElementById("tester").innerHTML="WORKED";
}
};
2
  • 1
    This if (variable1 = 7) { should be if (variable1 == 7) {. The first one does assignment, the second does comparison. Consider using a JavaScript validator to look for these sorts of bugs. jshint.com Commented Feb 23, 2013 at 17:16
  • ...also, keep your browser's developer console open during development. You'll see the TypeError resulting from the issue @j08691 described below. Commented Feb 23, 2013 at 17:19

2 Answers 2

2

Two things. First if (variable1 = 7) should be if (variable1 == 7). Second, call your function after the element is loaded in the DOM.

<!DOCTYPE html>
<html>
    <head>
    <script src="IntegrationCalculator.js"></script>
</head>
<body>
    <p id="tester">
        Didn't work :(
    </p>
    <script>
        integrationCalculator(7);
    </script>
</body>

jsFiddle example

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

1 Comment

Hope it helped. If you feel this answered your question, you may want to mark it as the accepted answer.
0

integrationCalculator is called before the tester paragraph is created. Therefore, an error is thrown and the execution of the script is stopped.

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.