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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox update input text field

Status
Not open for further replies.

kinnon

Programmer
Dec 21, 2005
9
0
0
GB
Hi,

I have a list of check boxes with group id's and group names for different locations. In my form, I have a groups field which needs to be populated with the group id's selected. So the text filed will eventually be a comma seporated list of group id's for a sql query, like
"'group1', 'group2', 'group3'"
If a tick is removed from any checkbox, then the associated group id is removed.

I'm not interested in iterating through the check values after the form has ben submitted, as the checks are not part of the form, and can be drilled down into sub groups.

Problem is ... er .... it dosn't work.

the javascript is as follows:
Code:
<script type="text/javascript">
<!--
function modifyText(id,val){
	var tar=document.getElementById(id);
	cbid = 'cb_'+val; // the checkbox id
	if(cbid.checked){
		// checkbox is checked, so add the text to the input text
		tar.value=val+', '+tar.value;
	}else{
		tar.value=tar.value.replace('/'+val+',/','');
	}
}
// -->
</script>

the example html is as follows:

Code:
<input type="text" name="filt_groups" id="filt_groups">
<table>
<tr>
	<td class="td1"><input type="checkbox" name="cb_ABER" value="ABER" onClick="modifyText('filt_groups','ABER');"></td>
	<td class="td1">ABER</td>
	<td class="td1">Aberdeen</td>
</tr>
<tr>
	<td class="td1"><input type="checkbox" name="cb_BEL" value="BEL" onClick="modifyText('filt_groups','BEL');"></td>
	<td class="td1">BEL</td>

	<td class="td1">Belfast</td>
</tr>
<tr>
	<td class="td1"><input type="checkbox" name="cb_BHAM" value="BHAM" onClick="modifyText('filt_groups','BHAM');"></td>
	<td class="td1">BHAM</td>
	<td class="td1">Birmingham</td>
</tr>
</table>

Please help!
Many thanks for viewing my post.


All the best,
kinnon
 
Ah!!,

Just noticed my mistake after posting this.

cbid = 'cb_'+val;
should be ...
var cbid = document.getElementById('cb_'+val)

The replace bit isnt workng though. so any hints would help.

Also, how would I deal with including the ' ' characters round the group id's?

Thanks again.

All the best,
kinnon
 
ok,

I've sussed it out.

my javascript looks like this now..

Code:
<script type="text/javascript">
<!--
function modifyText(id,val){
	var tar=document.getElementById(id);
	var cbid = document.getElementById('cb_'+val)
	if(cbid.checked){
		// checkbox is checked, so add the text to the input text
		if (tar.value==''){
			tar.value='\''+val+'\'';
		}else{
			tar.value='\''+val+'\', '+tar.value;
		}
	}else{
		tar.value=tar.value.replace('\''+val+'\', ','');
		tar.value=tar.value.replace('\''+val+'\'','');
	}
}
// -->
</script>

hope this is usefull to anyone else out there.

All the best,
kinnon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top