0

I would like to convert the event handler to a jquery style click event but it doesnt seem to like passing the event through, perhaps its because its not an anonymous function anymore?

        // variables
        var faqOne = document.getElementById("faqOne");
        var $hiddenOne = $(".faqOneHidden");


        // javascript event handler works!
        faqOne.addEventListener("click", function(e){
        showFaqOne.showClickedFaq(e);
        }, false);
        // javascript event handle - doesnt work!
        $("#faqOne").click(function(){
            showFaqOne.showClickedFaq(e);
        });
        // constructor
        function DisplayQFaqs(link, faq){
            this.link = link;
            this.faq = faq;
        }
        // method prototype
        DisplayQFaqs.prototype.showClickedFaq = function(e){
                var el = e.currentTarget;
                if(el === this.link) {

                   this.faq.toggle("slow", function(){
                   });
                }
        };
        // new DisplayQFaqs Objects
        var showFaqOne = new DisplayQFaqs(faqOne,$hiddenOne);
3
  • Do you know if the handler function is called? Maybe check it by putting an alert("a"); there? Commented Apr 28, 2015 at 8:39
  • fiddle: jsfiddle.net/rgthree/93bm0gs3 Commented Apr 28, 2015 at 8:40
  • The fiddle was not importing jQuery. This works - jsfiddle.net/93bm0gs3/2 Commented Apr 28, 2015 at 8:43

1 Answer 1

5

Your e is undefined inside

$("#faqOne").click(function(){
        showFaqOne.showClickedFaq(e);
    });

Change it to

   $("#faqOne").click(function(e){//Now e is there
        showFaqOne.showClickedFaq(e);
    });
Sign up to request clarification or add additional context in comments.

Comments

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.