5
Answers

jQuery checkbox always returns 'undefined' using foreach

Mani Kandan

Mani Kandan

8y
518
1
I am trying to implement a code to get checkbox list attr('name') while return 'undefind' word. What is the reason for this and How this one remove?
 
  jQuery(document).on("change", "#chkdaypart", function () {
var daypartText;
$(this).each(function (e) {

if (this.checked) {

daypartText = daypartText + $(this).attr('name');

if (daypartText == "undefinedDaytime–Weekday(06:26:00-17:25:00)") {
$('#daysTD').html('')
$('#daysTD').html('<ul><li><input type="checkbox" class="chkWeek" id="chkAllWeek" checked />All</li><li><input type="checkbox" checked value="Mon" name="Monday" class="chkWeek" id="chkWeek1" />Monday</li><li><input type="checkbox" checked value="Tue" name="Tuesday" class="chkWeek" id="chkWeek1" />Tuesday</li><li><input type="checkbox" checked value="Wed" name="Wednesday" class="chkWeek" id="chkWeek1" />Wednesday</li><li><input type="checkbox" checked value="Thu" name="Thursday" class="chkWeek" id="chkWeek1" />Thursday</li><li><input type="checkbox" checked value="Fri" name="Friday" class="chkWeek" id="chkWeek1" />Friday</li></ul>')
}
else if (daypartText == "undefinedDaytime-Weekend(06:26:00-17:25:00)") {

$('#daysTD').html('');
$('#daysTD').html('<ul><li><input type="checkbox" checked class="chkWeek" id="chkAllWeek" />All</li><li><input type="checkbox" checked value="Sun" name="Sunday" class="chkWeek" id="chkWeek1" />Sunday</li><li><input type="checkbox" checked value="Sat" name="Saturday" class="chkWeek" id="chkWeek1" />Saturday</li></ul>');
}

}
else {
$('#daysTD').html('');
$('#daysTD').html('<ul><li><input type="checkbox" checked class="chkWeek" id="chkAllWeek" />All</li><li><input type="checkbox" checked value="Sun" name="Sunday" class="chkWeek" id="chkWeek1" />Sunday</li><li><input type="checkbox" checked value="Mon" name="Monday" class="chkWeek" id="chkWeek1" />Monday</li><li><input type="checkbox" checked value="Tue" name="Tuesday" class="chkWeek" id="chkWeek1" />Tuesday</li><li><input type="checkbox" checked value="Wed" name="Wednesday" class="chkWeek" id="chkWeek1" />Wednesday</li><li><input type="checkbox" checked value="Thu" name="Thursday" class="chkWeek" id="chkWeek1" />Thursday</li><li><input type="checkbox" checked value="Fri" name="Friday" class="chkWeek" id="chkWeek1" />Friday</li><li><input type="checkbox" checked value="Sat" name="Saturday" class="chkWeek" id="chkWeek1" />Saturday</li></ul>')

}
});
});
 
 
 
And 'chkdaypart' id contain list of checkboxlist which return list of names like this:-
 
<ul>
<li>
<input type="checkbox" id="chkAlldaypart" />All</li>
@foreach (var items in ViewData["daypart"] as List<SelectListItem>)
{
<li>
<input type="checkbox" name="@items.Text" value="@items.Value" class="chkclass_daypart" id="chkdaypart"/>
@items.Text
</li>
}
</ul> 
 
 
 
 
Answers (5)