Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm using jQuery in my asp.net mvc application, in this i have created some checkboxes like
JavaScript
$("#dvModules").append('<span>&lt;input type="checkbox" id=Module_' + jsModulesData[ctr].Code + ' name="' + jsModulesData[ctr].Name + '" value="' + jsModulesData[ctr].Code + '"&gt; ' + jsModulesData[ctr].Name + '</span><br />');


I checked some of them and stored its values in database, when i enter it to this page again, i got the values from the database, but i could not check the corresponding checkboxes with their values my code is below:
JavaScript
var arrModuleIDs = SelectedModuleIDs.split(',');
$(document).ready(function () {
            SelectedModuleIDs = '@ViewBag.SelectedModuleIDs';
            var arrModuleIDs = SelectedModuleIDs.split(',');
            for (var i = 0; i < arrModuleIDs.length; i++) {
                  $("#Module_" + arrModuleIDs[i] + "").attr('checked', true);
//Here the arrmoduleIDs is the values from database.
                    }

here the loop is performing well but the checkbox is not checked, can anyone help me to resolve this.
Posted
Updated 13-May-12 23:34pm
v2
Comments
Sandeep Mewara 14-May-12 5:38am    
You debugged and made sure that the workflow of the implementation is correct? Control id's are as expected?
ZurdoDev 14-May-12 8:24am    
You likely just need to debug it and see what is happening. More than likely, "#Module_" + arrModuleIDs[i] + "" is not an actual ID of a control.

1 solution

hi karthime

I think u are trying to check the checkbox before it is created bcz you r doing it in document.ready and after that u r appending checkbox to div.

so after appending checkbox to the div then you have check the checkbox with the database values wt exactly u made it.

<pre lang="Javascript">

 $("#dvModules").append('<span><input type="checkbox" id=Module_' + jsModulesData[ctr].Code + ' name="' + jsModulesData[ctr].Name + '" value="' + jsModulesData[ctr].Code + '"> ' + jsModulesData[ctr].Name + '</span><br />');


//After appending checkbox u can loop the ids 

  SelectedModuleIDs = '@ViewBag.SelectedModuleIDs';
            var arrModuleIDs = SelectedModuleIDs.split(',');
            for (var i = 0; i < arrModuleIDs.length; i++) {
                  $("#Module_" + arrModuleIDs[i] + "").attr('checked', true);
//Here the arrmoduleIDs is the values from database.
                    }



Happy coding ..... :)
Seshu
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900