0
this.slideUpComm = function (){
    $("#Day-events-content p").addClass("hidden").slideUp("fast");
} 
this.showhideEvents = function() {
    $("#Day-events-content").on("click", ".eventsToggle", function() {
        var obj = $(this).next();
        if ($(obj).hasClass("hidden")) {
            slideUpComm();
            $(obj).removeClass("hidden").slideDown();
        } else {
            $(obj).addClass("hidden").slideUp();
        }
    });
}

I would like to use slideUpComm as function that I include on different events but console return Uncaught ReferenceError: slideUpComm is not defined. How I should pass function to function ? Should I use callback?

function dateObj() {
this.d = new Date();
this.day = this.d.getDate();
this.numDay = this.d.getDay();
this.month = parseInt(this.d.getMonth());
this.year = this.d.getFullYear();

this.slideUpComm = function (){

} 
this.showhideEvents = function() {

});
}
}

My Object look like above.

1
  • 5
    What is this, and how are you calling the function? Commented Dec 16, 2013 at 9:24

3 Answers 3

2

The problem is slideUpComm is a member of an object... so you need to use the object reference to invoke the method

//create a closure varaible to hold the reference to the instance
var self = this;
this.slideUpComm = function (){
    $("#Day-events-content p").addClass("hidden").slideUp("fast");
} 
this.showhideEvents = function() {
    $("#Day-events-content").on("click", ".eventsToggle", function() {
        var obj = $(this).next();
        if ($(obj).hasClass("hidden")) {
            //slideUpComm is a instance property so access it using the instance
            self.slideUpComm();
            $(obj).removeClass("hidden").slideDown();
        } else {
            $(obj).addClass("hidden").slideUp();
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You for Your help.I would like to ask is this way that I used it's appropriate of creating obj in JS? I mean best practice.
1

slideUpComm is a function of dateObj, you can not directly invoke the function. So to invoke a function you need to create an instance of function/object

var a = new dataObj();

then you can invoke the function using

a.slideUpComm()

1 Comment

@arun-p-johny answer is correct, my answer was relevant when function invocation was not added to the question
0

Could this not be reduced to:

$("<object>").slideToggle();

?

1 Comment

I think it's going to be shorter and clean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.