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.

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

Change

var x = +y + +z;

to

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

Done~

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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