Sumit
Sumit

Reputation: 1

insert checkbox checked value to textbox in asp.net?

I have a 45 checkboxes in webform with values 1 to 45 i want ...to insert checkbox checked value to textbox in comma seperated string as in ascending order as 1,2,3,4,5 ...if checkbox1, checkbox2, checkbox3, checkbox4 and checkbox5 is checked...if these checkboxes will be unchecked then the inserted value in textbox will be removed 1 by one respectively. ..

hwo to do this using vb.net or jquery or javascript ..

Upvotes: 0

Views: 3201

Answers (1)

Rion Williams
Rion Williams

Reputation: 76547

Since this sounds like homework - I'll just provide you with some instructions:

  • Use a split to separate your comma-delimited list into an array
  • Use jQuery's each() method on the array to iterate through the checked
  • Then use a $(this).attr('checked',true) on each of those values

Updated with Code and Example:

$(document).ready(function()
{
   var stringOfNumbers = "1,3,5,7";
   var split = stringOfNumbers.split(',');
   $.each(split, function(index, value)
   { 
       $("input[id='"+value+"']").attr('checked',true);
   });
});

Function

    function CheckTheseBoxes(stringOfNumbers)
    {
       var split = stringOfNumbers.split(',');
       $.each(split, function(index, value)
       { 
           $("input[id='"+value+"']").attr('checked',true);
       });
    }

Code Explanation:

The stringOfNumbers holds your comma-delimited string with all of your numbers to select, the stringOfNumbers.split(',') separates all of the values in the string and stores them in an Array. The $.each() method iterates through all of the values in the splitArray (the values that will determine which checkboxes are checked.

Inside the loop - a jQuery selector is built specifying the selection of an "input" where id is equal to value, value being the id to select. Finally, the .attr('checked',true) actually selects the checkbox.

Working Demo:

Working Demo

Links regarding jQuery and getting started with jQuery

Beginning with jQuery - A Solid Foundation

jQuery.com

Upvotes: 3

Related Questions