Jason
Jason

Reputation: 462

How to convert javascript to jquery + use it in Wordpress

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?

Upvotes: 0

Views: 281

Answers (1)

vinayakj
vinayakj

Reputation: 5681

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; 
}

Upvotes: 1

Related Questions