Reputation:
It is possivle chaining this code in jquery? or how I could improve?
$(document).ready(function() {
$("ul li:nth-child(2) .collapse-styled").addClass('in');
$("ul li:nth-child(2) .collapse-link a").removeClass('collapsed');
});
Upvotes: 0
Views: 22
Reputation: 15356
No. You cannot chain both statements as the two elements are on separate branches from each other. BUT, I would suggest saving the li
to shorten the search time like so:
$(document).ready(function() {
var $li = $("ul li:nth-child(2)");
$li.find(".collapse-styled").addClass('in');
$li.find(".collapse-link a").removeClass('collapsed');
});
Note:
Technically, you CAN chain them by going up the document tree but it would be highly inefficient. For example (if you have only one li
parent):
$("ul li:nth-child(2) .collapse-styled").addClass('in').closest("li").find(".collapse-link a").removeClass('collapsed');
I reccoment you don't use it.
Upvotes: 0
Reputation: 772
$(document).ready(function() {
var $listElement = $("ul li:nth-child(2)");
$listElement.find(".collapse-styled").addClass('in');
$listElement.find(".collapse-link a").removeClass('collapsed');
});
Upvotes: 1