0

I did a simple HTML page which has JavaScript as following:

<script>
  function myFunction() {
    var y = document.querySelector('input[name="mum1"]:checked').value;  
    var z = document.querySelector('input[name="num2"]:checked').value;
    var x = +y + +z;
    document.getElementById("total").innerHTML = x;
  }
</script>

<button onclick="myFunction()">Try it</button>

It works fine by itself. When I add it to a WordPress website(add HTML in a page). The error message comes like this:

Uncaught SyntaxError: Unexpected token 
Uncaught ReferenceError: myFunction is not defined

I guess this cause by JavaScript conflict, my WordPress website use jquery.

How to solve the problem.

4
  • You've added it how exactly? In a page template?
    – Milo
    Commented Nov 10, 2015 at 4:42
  • yeah, I mean in a page template.
    – James Xu
    Commented Nov 10, 2015 at 4:58
  • Where in the page template did you add this? Commented Nov 10, 2015 at 20:01
  • Create a new empty template which get content from THE PAGE. Then paste the code to Page in wordpress.
    – James Xu
    Commented Nov 10, 2015 at 22:04

1 Answer 1

0

Change

var x = +y + +z;

to

var x = Number(y) + Number(z);

Done~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.