Reputation: 6554
I am trying to make the coding a lot easier for me and so I assigned a global.
var parent = $(this).parent().parent().parent();
var parentModule = $(this).parent().parent().parent().parent();
And I use these through out my code to make $(this) a lot easier and save on all the parents. Though $(this) would technically be universal being within that certain event (click,hover, and so forth)
Is there a way of actually doing this as I believe it is not possible like the way I write it. maybe a function or something?
var parent, parentModule = null;
function getParents(e){
parent = $(e.currentTarget).closest(".module");
parentModule = $(e.currentTarget).closest(".module").parent();
}
$(close).on('click',function (e) {
getParents(e);
if (parentModule.hasClass('open')) {
var a = ReadCookie('ToHide');
if (a.split(",").length === 0) {
KillCookie('ToHide');
var b = "#"+parent.attr("id") + " #"+parentModule.attr("id");
SetCookie('ToHide', b, 100);
} else {
var d = a + "," + "#"+ parent.attr("id") + " #"+parentModule.attr("id");
KillCookie('ToHide');
SetCookie('ToHide',d, 100);
}
if(animate===true){
parentModule.fadeOut(function(){
checkIfVisible();
});
Upvotes: 1
Views: 80
Reputation: 2205
If you want to set the values of those global variables using a function, try this:
var parent, parentModule = null;
function getParents(e){
parent = $(e.currentTarget).closest(".yourParentSelector");
parentModule = $(e.currentTarget).closest(".yourParentModuleSelector");
}
$("button").click(function(e){
getParents(e);
//do whatever you want with parent and parentModule variables
});
Use closest()
instead of parent()
Here's the FIDDLE
Upvotes: 1