I have the following code working -
$('.sectionHeader:contains(Product Specific Documents)').next('div').find('table').find('input[type="checkbox"]').attr('checked', true);
var attr = $('.sectionHeader:contains(Product Specific Documents)').next('div').next('div').attr('id');
if(typeof attr !== undefined && attr !== false) {
$('.sectionHeader:contains(Product Specific Documents)').next('div').next('div').find('table').find('input[type="checkbox"]').attr('checked', true);
}
It basically selecting/checking checkboxes inside a specific table(s). It works fine but looks ugly.
I'd like to replace it with a while loop. The idea behind while loop is -
Find the required header (of table) and next DIV after that header is assigned to a variable. Then I check in a loop if id attribute exists and if yes, then I search for a table inside it then checkboxes inside and selecting them. Then I'm reassigning a variable adding next DIV and checking it in while loop.
So, the code looks like -
var at = $('.sectionHeader:contains(Product Specific Documents)').next('div');
while (typeof at.attr('id') !== undefined || at.attr('id') !== false) {
at.find('table').find('input[type="checkbox"]').attr('checked', true);
at = at.next('div');
}
But it doesn't work / it doesn't do the same as my initial code.
var at = $('.sectionHeader:contains(Product Specific Documents)').next('div');
for (var idx = 0; idx < 4 && (typeof at.attr('id') != 'undefined' && at.attr('id') != null); ++idx) {
at.find('table').find('input[type="checkbox"]').attr('checked', true);
at = at.next('div');
}
//This for loop works
I also found why while loop wasn't working - because the check for null was important -
var at = $('.sectionHeader:contains(Product Specific Documents)').next('div');
while (typeof at.attr('id') !== 'undefined' && at.attr('id') != null) {
at.find('table').find('input[type="checkbox"]').attr('checked', true);
at = at.next('div');
}
//This while also works
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.