• I’ve made a simple JavaScript calculator where the user selects two variables in a table, which are assigned values dependent on their choice, and then click an OnClick button tag below the table giving them an answer below the button.

    ‘calc.js’ is the JavaScript file that does the calculation. Inside calc.js the main function calculate() is called by the button on the page that user sees: <button onclick="calculate()">Calculate</button>.

    The JavaScript works, I currently have it in a standalone html file, which I’ve included on a WordPress page using an iframe. Fine for now for users who are none the wiser but I’d rather have the calculator actually in my WordPress site.

    So I put this in the header.php file:

    <?php if (is_page(244)) {
    echo '<script type="text/javascript" src="/calc.js"></script>';
    } ?>

    And then I put the table code into the editor for the page, and the table itself shows up fine. But when I enter my two variables and click the button, nothing happens.

    So can any of you guys help me out?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    You can use the same debugging techniques from your standalone application as you can in WordPress. Use your browser developer tool to access the console log. Look at the information there to decipher what’s going on. Just looking at the header.php code right now I can tell you that the URL to the file is probably wrong.

    Thread Starter baldonius

    (@baldonius)

    The url to the file is correct. I have also tried src="calc.js" and the full path to the file. The Chrome developer tool gives no particular notifications about the JavaScript.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Hopefully you can link the page with the issue.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘JavaScript calculator and form’ is closed to new replies.