3

dear all.I want to use only one textfield for different results. what should I do if I want after pressing a few times a checkbox or another checkbox the results will appear sequentially in the textfield.for example:

<input type="checkbox" id="see" value="a">
<input type="checkbox" id="saw" value="b">

<input type="text" id="field">
<input type="button" id="show">

then i do something like:

 1. click "see"
 2. click "show"
 3. click "see"
 4. click "show"
 5. click "saw"
 6. click "show"
 7. click "saw"
 8. click "show"
 9. click "see"
 10. click "show"

then show results after click show button at text field:

aaba..and so on if any additional
mleko
  • 11,650
  • 6
  • 50
  • 71
klox
  • 2,089
  • 11
  • 38
  • 65

2 Answers2

2

As simple as:

var values = "";
$('#see, #saw').click(function() {
  values += $(this).val();
});
$('#show').click(function() {
  $("#field").val(values);
});

to only allow the checked=true checkboxes just:

var values = "";
$('#see, #saw').click(function() {
 if($(this).is(':checked'))
    values += $(this).val();
});
$('#show').click(function() {
  $("#field").val(values);
});

Edited: to add the functionality to the Show Button

See working fiddle

Garis M Suero
  • 7,974
  • 7
  • 45
  • 68
1

Try something like this:

$('input:checkbox').click(function() {
  $('#field').val($('#field').val() + $(this).val());
});​

Here's a working fiddle.

If you need a variant where you only record the clicks that create a checked state, just wrap the assignment in:

if( $(this).is(':checked') ) {...}
Ken Redler
  • 23,863
  • 8
  • 57
  • 69