0

I have written the following (working) javascript

function calc(A,B,SUM) { 
  var one = Number(A); 
  var two = Number(document.getElementById(B).value);  
  if (isNaN(one)) { alert('Invalid entry: '+A); one=0; } 
  if (isNaN(two)) { alert('Invalid entry: '+B); two=0; } 
  document.getElementById(SUM).value = (one/(((two*0.6)/2.4)/12))*100; 
}

However, I need to convert it to jquery to work with Wordpress. I have a .js file properly enqueued.

Everything I do just creates errors. What resources are out there to explain how to convert it to jquery and how to make they jquery work in wordpress?

5
  • 2
    You don't have to convert it to jQuery, jQuery is javascript, and the code should work perfectly fine in Wordpress. Commented Nov 21, 2014 at 6:30
  • It works fine outside of WP and I thought in the past I remember something about needing to do something special. Commented Nov 21, 2014 at 6:37
  • Yes unless you have too much cross-browser problems or no animations needed, stick with javascript code, which would be faster, as internally jQuery converts the code to javascript. Commented Nov 21, 2014 at 6:37
  • I am not a javascript guy by any means, but it works in my code editor and jsfiddle. The script is loading on the Wordpress page properly as I can see it via the code inspector . . . so as it's written is should work on a Wordpress page? Commented Nov 21, 2014 at 6:42
  • Hey Jason, jQuery is facade to javascript, the library based on javascript, created to ease the development process for developers by giving easy and simple APIs. Commented Nov 21, 2014 at 6:49

1 Answer 1

1

Stick with javascript unless jQuery is needed. Otherwise below is equivalent jQuery code.

function calc(A,B,SUM) { 
  var one = Number(A), two = Number($("#"+B).val());  
  if (isNaN(one)) { alert('Invalid entry: '+A); one=0; } 
  if (isNaN(two)) { alert('Invalid entry: '+B); two=0; } 
  $("#"+SUM).value = (one/(((two*0.6)/2.4)/12))*100; 
}
Sign up to request clarification or add additional context in comments.

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.