Jquery calculation question

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Jquery calculation question

Post 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? :/
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Jquery calculation question

Post 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.
Kennydin
Posts: 39
Joined: Sat Oct 31, 2009 12:47 am

Re: Jquery calculation question

Post 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?
PHP/Python (amongst other things) Person - Big Fan of MVC Architecture. Love CakePHP and CodeIgniter.
Post Reply

Return to “Advanced Help and Support”