Page 1 of 1

Jquery calculation question

Posted: Sun May 15, 2011 10:44 am
by ConceptDestiny
Hey guys,

Had something bugging me for ages. I'm pretty newbie to jquery, so I was wondering if you guys might have an ideas. First the code:

Code: Select all

<script type="text/javascript">
	$(document).ready(
		function (){
			// automatically allocates level points
			$("input[id^=levelsum]").sum("keyup", "#totalpoints");			
		}
	);
</script>
I'm trying to add a calculation directly onto the levelsum input (i.e. levelsum*10/0.5), then output it into #totalpoints. Any ideas out there? :/

Re: Jquery calculation question

Posted: Sun May 15, 2011 1:20 pm
by Xaleph
I guess you need the awnser to be output. If the sum is correct ( which i don`t know know , mainly because you want (level * 10) /0.5 or something else?

Anyway, i guess you should fetch the awnser and write it as output. So not direct, but indirect.

Re: Jquery calculation question

Posted: Fri May 27, 2011 10:07 am
by Kennydin
Im not exactly sure what're you getting at here but this is what I'd do If I wanted to calculate level points through jquery...

Code: Select all

   $(document).ready(function (){
          points = $('#levelsum').val();        
          totalpoints = (levelsum*10)/5;
          $("#totalpoints").val(totalpoints);         
      });

You need to do the calculation and then set the value of the input fields... using the .val() method... the $('#totalpoints') refers to <input type='text' id='totalpoints'> or if you want it as a span you could do this.. <span id='totalpoints'></span> and then replace the last line of code above with $('#totalpoints').text(totalpoints);

If this is what you mean?