...

View Full Version : onclick handler in checkbox



hyefive
04-07-2007, 03:14 AM
Hi,

Still learning js, so bear with me!

I have a form that enables a user to add optional features to a plan using checkboxes. When they check or uncheck the checkbox for the add-on, the Total Monthly Cost amount is updated in a span below. That part of it seems to be working fine. I'm having trouble with the onclick event however, in that if checked, it won't uncheck when clicked upon, and if I click one the other one gets checked also.

Any help will be greatly appreciated!

<script>

---other script that deals with display: none or inline for two of the checkbox/label combos below (FEATURED_CL and OFF_CL)---

function totalcost(addonselected) {
var tc = Number(document.getElementById('prices').getElementsByTagName('span')[1].firstChild.nodeValue);
addonselected.checked = !(addonselected.checked);
if (document.getElementById('ENOTE_CL').checked = true) {
var addon1 = Number(document.getElementById('prices').getElementsByTagName('span')[2].firstChild.nodeValue);
}
if (document.getElementById('FEATURED_CL').checked = true) {
var addon2 = Number(document.getElementById('prices').getElementsByTagName('span')[3].firstChild.nodeValue);
}
else if (document.getElementById('OFF_CL').checked = true) {
var addon2 = Number(document.getElementById('prices').getElementsByTagName('span')[3].firstChild.nodeValue);
}
tc = tc + addon1 + addon2;
document.getElementById('items').getElementsByTagName('span')[0].firstChild.nodeValue = tc;
}
</script>

<body>
<form>

---other stuff---

<div id="items">
<input type="checkbox" id="setup" name="products" value="setup" checked="checked" disabled="disabled" class="check" /><label for="setup">SETUP FEE<br /></label>
<input type="checkbox" id="search" name="products" value="search" checked="checked" disabled="disabled" class="check" /><label for="search">SEARCH<br /></label>
<input type="checkbox" id="ENOTE_CL" name="products" value="ENOTE_CL" class="check" onclick="totalcost(this);" /><label for="ENOTE_CL">EMAIL NOTIFIER<br /></label>
<input type="checkbox" id="FEATURED_CL" name="products" value="FEATURED_CL" class="check" onclick="return totalcost(this);" /><label for="FEATURED_CL">FEATURED ADS<br /></label>
<input type="checkbox" id="OFF_CL" name="products" value="OFF_CL" style="display:none;" class="check" onclick="return totalcost(this);" /><label for="OFF_CL" style="display:none;">OFFICE SOLUTION<br /></label>
<br />
Total Monthly Cost: $<span>22.95</span><br />
</div>
<div id="prices">
$<span>59.00</span> OT<br />
<span>22.95</span>/mo<br />
+<span>10.00</span>/mo<br />
+<span>10.00</span>/mo<br />
</div>

---other stuff---

</form>
</body>

glenngv
04-07-2007, 03:22 AM
Why do you have this?

addonselected.checked = !(addonselected.checked);
That will make the checkbox "unclickable". And also the equality symbol in the condition is == and not =.

if (document.getElementById('ENOTE_CL').checked == true) {
var addon1 = Number(document.getElementById('prices').getElementsByTagName('span')[2].firstChild.nodeValue);
}

hyefive
04-07-2007, 03:33 AM
Thank you for the reply.

I implemented your changes, along with a couple other small fixes, and it's now functional. I appreciate it!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum