0

I already have 3 checkboxes and 1 textbox in asp.net webform when i check 1and 2 checkbox then result in textbox will be 1,2

I have the following code :

<script type="text/javascript">
    function Test(control, value) {
        var str = '';
        if (control.checked) {
          str = value + '\n' + $('#<%=txtTest.ClientID %>').val();
        }
        else {
            str = $('#<%=txtTest.ClientID %>').val().replace(value + '\n', '');  
        }
        $('#<%=txtTest.ClientID %>').val(str);
    }
</script>

to do this......

But the problem in this code ...was when i check 3 and 2 checkbox then result in the textbox is 3,2,

But i want it appear as 2,3 only ...

Can anybody reedit this code ....... ??????

I would be thank ful to u ..............

Pointy
  • 405,095
  • 59
  • 585
  • 614
user515609
  • 39
  • 2
  • 12

2 Answers2

1

What if you just reverse the output?

$('#<%=txtTest.ClientID %>').val().split(",").reverse().join(",");

rwilliams
  • 21,188
  • 6
  • 49
  • 55
  • It just reveses the values of your checkboxes. So "3,2" becomes "2,3". You probably want to use it right before you submit the form. – rwilliams Nov 09 '10 at 13:58
  • But if the user checks 2,3, user still wants 2,3 to display correct? – Kamal Nov 09 '10 at 14:36
  • Good call. I was trying to keep their original code usable. That said it doesn't appear the OP wants to put any effort into figuring this out so I'm not going to put any more effort in my answer :P – rwilliams Nov 09 '10 at 15:58
1

This function is triggered when user clicks check box. So if you click 2 & 3 by this order, them there is everything right?

So user "each" sentence:

<script type="text/javascript">
    function Test(control, value) {
     var str = '';
     $('#<%=txtTest.ClientID %> :checked').each(function() {
       str = str + ', ' $(this).val();
     });
     $('#<%=txtTest.ClientID %>').val(str);

</script>
jmav
  • 3,119
  • 4
  • 27
  • 27