How to make this code a little more compact
I have a construction that checks if a checkbox is checked (who can use more words 'check' so close to each other? =)) when the document loads and if it's checked, some rows in the table get hidden. It looks like this:
if (jQuery('#navbar_disabled').is(':checked')) {
jQuery('#navbar_disabled').parents('tr').prev().hide();
jQuery('#navbar_disabled').parents('tr').prev().prev().hide();
}
I am sure there is a quick nice way to开发者_高级运维 get rid of those 2 last '#navbar_disabled' and use something like 'this' or whatever instead. I know I can use something like var navbar_checkbox_disabled=jQuery('#navbar_disabled')
and deal with it but I am looking for a more cute solution, if it is there. Thanks.
UPDATE: Why I am asking for the other solution than just assigning it to a new variable is that I need to use it on many elements like this:
jQuery('#company_address_disabled, #sociallinks_disabled, #contacts_mail_disabled, #navbar_disabled')
Why not use it as a filter, which will make the jQuery object empty if it's not checked:
jQuery('#navbar_disabled:checked').parents('tr').prev().hide().prev().hide();
something like this maybe!?
$tr = $('#navbar_disabled').parents('tr').prev();
if ($('#navbar_disabled').is(':checked')) {
$tr.hide();
$tr.prev().hide();
}
EDIT:
do you thinked to add class on theese elements!? i suppose your code look something like this:
<tr class='hide_me'><td>prev</td></tr>
<tr class='hide_me'><td>prev</td></tr>
<tr><td><input id="navbar_disabled" /></td></tr>
if ($('#navbar_disabled').is(':checked')) {
$('.hide_me').hide();
}
精彩评论