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

Concatenate selections from list using checkboxes 1

Status
Not open for further replies.

coders4hire

IS-IT--Management
Dec 31, 2003
22
US
I need to utilize checkboxes on one of my pages where the user selects the content that he/she wants to obtain. Rather than submit those values independently, I want to concatenate the answers into a single value.

Code:
<table>
<tr><td><INPUT type="checkbox" id=news name=news value=news></td><td>News</td></tr>
<tr><td><INPUT type="checkbox" id=events name=events value=events></td><td>Events</td></tr>
<tr><td><INPUT type="checkbox" id=promos name=promos value=promos></td><td>Promotions</td></tr>
<tr><td><INPUT type="checkbox" id=info name=info value=info></td><td>Info</td></tr>
<tr><td><INPUT type="checkbox" id=tech name=tech value=tech></td><td>Technical</td></tr>
<tr><td><INPUT type="checkbox" id=dev name=dev value=dev></td><td>Developer</td></tr>
</table></P>

I would like a delimiter in between every option, selected or not. So if events, tech, and dev are checked... I would like to submit a value for MYVAL of:
Code:
<input type=hidden name=myval value="null;events;null;null;tech;dev">

Where null is an unselected value and the value is the selected value.

Doug
 
Try something like this:

Code:
<script type="text/javascript">
function changevalues()
{
var myvalvalue = '';

//list of checkboxes to evaluate
var cbox = ['news', 'events', 'promos', 'tech', 'info', 'dev'];

for (var ci=0;ci<cbox.length;ci++)
  {
  var onecbox = document.getElementById(cbox[ci])
  var checked = onecbox.checked;
  if (ci > 0) myvalvalues += ';';
  if (checked)
    {
    myvalvalue += onecbox.value;
    }
  else
    {
    myvalvalue += 'null';
    }
  }
document.getElementById('myval').value = myvalvalue;
}
</script>

<table>
<tr><td><INPUT type="checkbox" id=news name=news value=news onclick="changevalues()"></td><td>News</td></tr>
<tr><td><INPUT type="checkbox" id=events name=events value=events onclick="changevalues()"></td><td>Events</td></tr>
<tr><td><INPUT type="checkbox" id=promos name=promos value=promos onclick="changevalues()"></td><td>Promotions</td></tr>
<tr><td><INPUT type="checkbox" id=info name=info value=info onclick="changevalues()"></td><td>Info</td></tr>
<tr><td><INPUT type="checkbox" id=tech name=tech value=tech onclick="changevalues()"></td><td>Technical</td></tr>
<tr><td><INPUT type="checkbox" id=dev name=dev value=dev onclick="changevalues()"></td><td>Developer</td></tr>
</table></P>
<input type=hidden name=myval value="null;events;null;null;tech;dev">

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top