Better way to "find parent" and if statements
Posted
by
Luke Abell
on Stack Overflow
See other posts from Stack Overflow
or by Luke Abell
Published on 2012-09-15T21:14:12Z
Indexed on
2012/09/15
21:38 UTC
Read the original article
Hit count: 437
jQuery
I can't figure out why this isn't working:
$(document).ready(function() {
if ($('.checkarea.unchecked').length) {
$(this).parent().parent().parent().parent().parent().parent().parent().removeClass('checked').addClass('unchecked');
}
else {
$(this).parent().parent().parent().parent().parent().parent().parent().removeClass('unchecked').addClass('checked');
}
});
Here's a screenshot of the HTML structure: http://cloud.lukeabell.com/JV9N (Updated with correct screenshot)
Also, there has to be a better way to find the parent of the item (there are multiple of these elements on the page, so I need it to only effect the one that is unchecked)
Here's some other code that is involved that might be important:
$('.toggle-open-area').click(function() {
if($(this).parent().parent().parent().parent().parent().parent().parent().hasClass('open')) {
$(this).parent().parent().parent().parent().parent().parent().parent().removeClass('open').addClass('closed');
}
else {
$(this).parent().parent().parent().parent().parent().parent().parent().removeClass('closed').addClass('open');
}
});
$('.checkarea').click(function() {
if($(this).hasClass('unchecked')) {
$(this).removeClass('unchecked').addClass('checked');
$(this).parent().parent().parent().parent().parent().parent().parent().removeClass('open').addClass('closed');
}
else {
$(this).removeClass('checked').addClass('unchecked');
$(this).parent().parent().parent().parent().parent().parent().parent().removeClass('closed').addClass('open');
}
});
(Very open to improvements for that section as well)
Thank you so much!
Here's a link to where this is all happening: http://linkedin.guidemytech.com/sign-up-for-linkedin-step-2-set-up-linkedin-student/
Update: I've improved the code from the comments, but still having issues with that first section not working.
$(document).ready(function() {
if ($('.checkarea.unchecked').length) {
$(this).parents('.whole-step').removeClass('checked').addClass('unchecked');
}
else {
$(this).parents('.whole-step').removeClass('unchecked').addClass('checked');
}
});
--
$('.toggle-open-area').click(function() {
if($(this).parent().parent().parent().parent().parent().parent().parent().hasClass('open')) {
$(this).parents('.whole-step').removeClass('open').addClass('closed');
}
else {
$(this).parents('.whole-step').removeClass('closed').addClass('open');
}
});
$('.toggle-open-area').click(function() {
$(this).toggleClass('unchecked checked');
$(this).closest(selector).toggleClass('open closed');
});
$('.checkarea').click(function() {
if($(this).hasClass('unchecked')) {
$(this).removeClass('unchecked').addClass('checked');
$(this).parent().parent().parent().parent().parent().parent().parent().removeClass('open').addClass('closed');
}
else {
$(this).removeClass('checked').addClass('unchecked');
$(this).parent().parent().parent().parent().parent().parent().parent().removeClass('closed').addClass('open');
}
});
© Stack Overflow or respective owner