Click here to Skip to main content
15,894,630 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

I am creating a view using MVC4 where I have to create controls dynamically based on the number of records I am receiving from database. I have two type of controls textbox and checkbox. Textbox value I am setting dynamically whatever I am gettingin my model. But I am stuck while setting checked attribute of checkbox. What I want is if I received feature_flag value Yes then for that row checkbox should be checked else uncheck.
Below is the code I have written:-
function OnSuccessSelectedModuleFeatureList(data) {
var appdata = data.applicationAdmin;
var row = "";
debugger;
$.each(appdata, function (index, item)
{
row += "
" + item.feature_name + "<input type=\"checkbox\" id=\"Check" + index + "\"><input type=\"text\" value=" + item.feature_value + ">
";
if (item.feature_flag == "Y")
{
var a = 'Check' + index;
$('#Check' + index).attr('checked', 'checked');
// $('#Check' + index).prop('checked', true);
}
else
{
$('#Check' + index).prop('checked', false);
}
});

$('#FlagAutomationModuleSelectedModuleFetures tbody').html(row);
$('#SelectedModuleNameStep2').html(SelectedModuleName)
}
for textboxes it is working but for checkboxes it is not working.
I would be grateful if anybody can help. Please reply if more details are required.

Thanks in advance.
Posted

try this:

if (item.feature_flag == "Y")
{

<input type=\"checkbox\" id=\"Check" + index + "\" checked="true">
}
else
{

<input type=\"checkbox\" id=\"Check" + index + "\" checked="false">
}
 
Share this answer
 
Thanks for solution...but adding a change for future reference.
The code should be like this
if (item.feature_flag == "Y")
{
row += "<input type=\"checkbox\" id=\"Check" + index + "\" checked=\"checked\">";
}
else
{
row += "<input type=\"checkbox\" id=\"Check" + index + "\" >";

}
 
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