Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If CheckBoxList item checked then show value in textbox how? 1

Status
Not open for further replies.

KingPacino

Programmer
Jul 21, 2003
53
GB
I have an asp:checkboxlist dynamically filled with values in c#, I need a javascript function to go through the checkboxlist items and if it is checked then put the value of the checkboxlist item into a textbox, I have found a function that seems to loop through the items ok but actually extracting the value of each checkboxlist item is proving very difficult for me.

Here is what I have, it is a kind of work around but is doing something vaguely similiar to what I want -

Code:
var controlIndex;
var element;
var numberOfControls = document.Form1.length;
   
for (controlIndex = 0; controlIndex < numberOfControls; controlIndex++)
   {
      element = document.Form1[controlIndex];
      
      if (element.type == "checkbox")
      {
         if (element.checked == true)
         {
            //A test to see if working
            //alert("Test"+element.value);
         }
      }
   }

any ideas would be very welcome, a lot of the solutions I have found on the net seem to be for the standard checkbox but have found very little so far as asp:checkboxlists'.

Thanks
 
Can't you just replace this:

Code:
//A test to see if working
//alert("Test"+element.value);

With this:

Code:
document.forms['Form1'].elements['textboxname'].value = element.value;

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
Yep thats cool it appears to be the right way but all that appears in the textbox at the mo is "on", which is not the checkboxes value, each checkboxes value is a path "c:/whatever/for_example" which is added dynamically to the checkboxlist in c# and so is not hard coded in the html, not sure what difference this makes but trying to give as much info as poss.

Any more ideas?

(I can get this to work in c# but needed something client side) here is my c# code for completeness -

Code:
for (int i=0; i<CBL2.Items.Count; i++)
{
 if (CBL2.Items[i].Selected)
 {
    TB3.Text += CBL2.Items[i].Value;		
 }
}

where CBL2 is my checkboxlist and TB3 the textbox
 
Can you please paste the html that you see when you do a view > source in the browser?

thanks...

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
I have pasted the code of the checkboxlist, as, correct me if I'm wrong, I thought you might be thinking, "does a 'value' actualy exist?" And your suspicions, if I was on the same wavelength, were right as there doesn't appear to be a value associated with the checkboxes, only a 'label for'.

Code:
<TD style="HEIGHT: 36px"><table id="CBL2" onClick="javascript:count()" bordercolor="CornflowerBlue" border="0" style="background-color:White;border-color:CornflowerBlue;border-width:2px;border-style:Groove;font-family:Arial;font-size:7pt;height:100%;width:810px;">
	<tr>
		<td><input id="CBL2_0" type="checkbox" name="CBL2:0" checked="checked" onclick="__doPostBack('CBL2$0','')" language="javascript" tabindex="3" /><label for="CBL2_0"> \PACKAGES\EMIS;</label></td><td><input id="CBL2_1" type="checkbox" name="CBL2:1" checked="checked" onclick="__doPostBack('CBL2$1','')" language="javascript" tabindex="3" /><label for="CBL2_1"> \SHARED\GVEVENTHANDLER;</label></td>
	</tr><tr>
		<td><input id="CBL2_2" type="checkbox" name="CBL2:2" checked="checked" onclick="__doPostBack('CBL2$2','')" language="javascript" tabindex="3" /><label for="CBL2_2"> \SHARED\TLB;</label></td><td><input id="CBL2_3" type="checkbox" name="CBL2:3" checked="checked" onclick="__doPostBack('CBL2$3','')" language="javascript" tabindex="3" /><label for="CBL2_3"> \SHARED\DSOAP2.1\SOURCE;</label></td>
	</tr><tr>
		<td><input id="CBL2_4" type="checkbox" name="CBL2:4" checked="checked" onclick="__doPostBack('CBL2$4','')" language="javascript" tabindex="3" /><label for="CBL2_4"> \SHARED\EMISDATASERVICELIBRARY;</label></td><td><input id="CBL2_5" type="checkbox" name="CBL2:5" checked="checked" onclick="__doPostBack('CBL2$5','')" language="javascript" tabindex="3" /><label for="CBL2_5"> \SHARED\XML;</label></td>
	</tr><tr>
		<td><input id="CBL2_6" type="checkbox" name="CBL2:6" checked="checked" onclick="__doPostBack('CBL2$6','')" language="javascript" tabindex="3" /><label for="CBL2_6"> \MC\PATIENTFIND;</label></td><td><input id="CBL2_7" type="checkbox" name="CBL2:7" checked="checked" onclick="__doPostBack('CBL2$7','')" language="javascript" tabindex="3" /><label for="CBL2_7"> \MC\USERMANAGER;</label></td>
	</tr><tr>
		<td><input id="CBL2_8" type="checkbox" name="CBL2:8" checked="checked" onclick="__doPostBack('CBL2$8','')" language="javascript" tabindex="3" /><label for="CBL2_8"> \MC\MEDICATION\COMMON;</label></td><td><input id="CBL2_9" type="checkbox" name="CBL2:9" checked="checked" onclick="__doPostBack('CBL2$9','')" language="javascript" tabindex="3" /><label for="CBL2_9"> \MC\MEDICATION;</label></td>
	</tr>
</table></TD>

In the c# code I give each checkboxlist item a different value and text so the path shown is not what is pasted into the textbox (just to make it a bit more confusing, no its to save space as some paths are long, and don't want to take too much space up on screen)
 
Hey King. You were correct in your assumptions. You have to understand though that there are all types of people on here - I can't always assume someone knows what they're doing ;)

Anyway, I think I figured out what you need. Take this simple example:

Code:
<html>
<head>
<script language="JavaScript">

<!--
function doIt(el) {
   var l = el.nextSibling;
   alert ( l.innerHTML );
}
-->

</script>
</head>

<body>

<table><tr>
  <td id="first_cell">
    <input type="checkbox" name="cb1" onclick="doIt(this);" /[red]><[/red]label for="cb1">Stuff</label>
  </td>
</tr></table>

</body>
</html>

You'd apply a very similar concept to your function.

Replace this:

Code:
//A test to see if working
//alert("Test"+element.value);
With this:
Code:
document.forms['Form1'].elements['textboxname'].value = element.nextSibling.innerHTML;

That should do the trick. Now, as for the red part I highlighted in red - make sure no space goes between those two characters, otherwise your code won't work (a space will be considered the next sibling).

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
Thats great thank you for all your help, I didn't mean to sound as if I was being clever before just trying to think ahead ;)

Appreciate all your help and the speed at which you replied especially helped!

Thanks again :)
 
anytime, I'm glad to help.

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top