checkboxlist and javascript

  • Thread starter Thread starter Stimp
  • Start date Start date
S

Stimp

I have a checkboxlist (chkMyList) which is created from a (name, value)
pair from a database table.

I have a read-only textbox which will be used to hold a total of
all the numerical values of each checkbox that is checked, and this
value will change dynamically via javascript when a box is ticked or unticked.

e.g.
<script language="javascript">
function GetCost() {
var iTotalCost;
iTotalCost = 0;

if (document.form.chkMyList_0.checked){
iTotalCost = iTotalCost + 15;}
if (document.form.chkMyList_1.checked){
iTotalCost = iTotalCost + 18;}

....etc...
document.form.txtTotalCost.value = iTotalCost;
}
</script>

<asp:checkboxlist id="chkMyList" runat="server" />
<asp:textbox id="txtTotalCost" runat="server" ReadOnly="True" />


...but when I try to add the 'onchange=GetCost();' to chkMyList in the
Page_Load():

chkMyList.Attributes.Add("onChange", "GetCost();")

...it doesn't add it to the individual checkboxes, but rather to the
<table> generated for the checkboxes by the checkboxlist function.

Hence it doesn't work.

Is there any way to add the javascript to each checkbox in the
checkboxlist as they are generated?

Also a better suggestion for the javascript function would be most
appreciated (Ideally I'd like a function that takes in the checkbox
value, rather than having to hardcode a load of 'if' statements etc)

Thanks in advance!
Peter
 
Hello Peter,

Would the following javascript work, perhaps:

var text_to_add;
function addValue(text_to_add) {
document.getElementById("the_textbox_id").value =
document.getElementById("the_textbox_id").value + text_to_add;
}

This just adds the value of the textbox to the new value. You would
just call the function in the checkbox onclick() event.

Chris S.
Implied By Design LLC.
http://www.impliedbydesign.com
Free Web Design Tools
http://www.impliedbydesign.com/free-software-scripts.html
 
Hello Peter,

Would the following javascript work, perhaps:

var text_to_add;
function addValue(text_to_add) {
document.getElementById("the_textbox_id").value =
document.getElementById("the_textbox_id").value + text_to_add;
}

This just adds the value of the textbox to the new value. You would
just call the function in the checkbox onclick() event.

Thanks! I'll give it a shot first thing in the morning.. any idea how I
could attach the 'onclick' to each checkbox as the checkboxlist is
generated?

Peter
 
Back
Top