/    Sign up×
Community /Pin to ProfileBookmark

while click on checkboxes price should add using javascript or jquery

Hello everyone,

I created one form in that i created some checkboxes like price1,price2,price3.

And one Textbox for displaying value using javascript (or) Jquery (or) Ajax

Now my problem is if i checked price1, the price1 value should display in Texbox.

And if i checked two Checkboxes(price1,price2) the both values should add and display in textbox.

Can anyone help me out.

Thanks
sumanth

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERFeb 22.2013 — Something like this?

See: http://www.javascriptsource.com/forms/auto-sum-checkboxes.html

Here's an updated version of the code...
<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Checkbox Summation &lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;div id="costEntry"&gt;
&lt;input type="checkbox" value="9.99" &gt;Game 1 ( 9.99)&lt;br&gt;
&lt;input type="checkbox" value="19.99"&gt;Game 2 (19.99)&lt;br&gt;
&lt;input type="checkbox" value="27.50"&gt;Game 3 (27.50)&lt;br&gt;
&lt;input type="checkbox" value="45.65"&gt;Game 4 (45.65)&lt;br&gt;
&lt;input type="checkbox" value="87.20"&gt;Game 5 (87.20)
&lt;/div&gt;
&lt;input type="text" id="totalcost" value=""&gt;


&lt;script type="text/javascript"&gt;
/* This script and many more are available free online at
The JavaScript Source!! http://www.javascriptsource.com
Created by: Jay Rumsey | http://www.nova.edu/~rumsey/ */
// Modified on: 2/22/2013

function UpdateCost() {
var sum = 0;
var sel = document.getElementById('costEntry').getElementsByTagName('input');
for (i=0; i&lt;sel.length; i++) {
if (sel[i].checked == true) { sum += Number(sel[i].value); }
} document.getElementById('totalcost').value = sum.toFixed(2);
}

window.onload = function() {
var sel = document.getElementById('costEntry').getElementsByTagName('input');
for (var i=0; i&lt;sel.length; i++) {
sel[i].onclick = function () { UpdateCost(); }
}
}
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @sumanth spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...