0

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 ..

1

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

why you are using 1,3,5,7 //// what to do if i use 45 checkboxes ?
That is your comma delimited string - that was just an example. You could either a.) Put your string there or b.) Use a function to take in your string and process it. I'll show you an example of the latter answer.
I updated the demo and added a function in there. Just type your string into the input and hit the button to check the boxes you typed in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.