Giri
Giri

Reputation: 4909

Can someone help me in jquery addition?

I would like to give points to my users who submit articles based on their articles word count.

But my page has many text area fields. Lets say message1,message2,message3

When the user fill textarea(message1,message2,message3) it will show like this in the bottom.

You have typed x words. Points per word : 0.2. Estimated point: xx

I also want to calculate overall points. So i would like to add points of message1,message2,message3 and display it as overall points.

I'm a jquery noob. So i'm not sure which variable i should call.

Here is the jfiddle code what i have so far.

Can someone help me? Thanks

Upvotes: 2

Views: 136

Answers (5)

csturtz
csturtz

Reputation: 6580

Your first problem is that you're not storing the scores from the individual text area fields so you have no values to add up for the total score. After you start storing this data, you just have to update the total score area when any of the individual scores change.

Check out this fiddle: http://jsfiddle.net/NFDQ3/6/

Upvotes: 1

Telmo Marques
Telmo Marques

Reputation: 5106

Keep the count of every Textfield in an array that you can use to compute the overall sum.

I've edited your code, see the demo here: http://jsfiddle.net/yZb7w/38/

Upvotes: 1

bfavaretto
bfavaretto

Reputation: 71918

You could trigger an event from inside getInfo, then bind to that event from outside your plugin to get the updated count from each textarea. Then, it's just a matter of keeping track of each count and adding them up. See jsfiddle (info object will be logged to the console).

Upvotes: 0

Bogdan Emil Mariesan
Bogdan Emil Mariesan

Reputation: 5647

Another solution would be to have something like:

  $('#testTextarea3').textareaCount(options3, function(data){
                $('#showData3').html("You have typed <b>" + data.words + "</b> words. Points per word : <b> 0.2</b>. Estimated point: <b>" + (0.2 * data.words) +"</b>");
                 $(this).data("points",(0.2 * data.words));

            });

This will store the points value per testTextarea. The next step you will do is have a totalScore textarea or some updating behavior for some component and extract stored scored from all the textAreas like:

   $('#testTextarea3').data("points");

Upvotes: 0

DarkAjax
DarkAjax

Reputation: 16223

you can declare a variable to save the value of each word count (initialized in 0):

var count1 = count2 = count3 = 0;

And then do something like:

count1 = data.words;
$('#showData4').html((count1+count2+count3)*0.2);

For each $('#testTextarea').textareaCount() you have, as you can see on this jsfiddle

Upvotes: 2

Related Questions